-1

第一个 XML

<?xml version="1.0"?>
<response>
<status>
<code>0</code>
</status>
<newsList>
<news>

<id>1</id>
<title>some</title>
<date>30.11.2011T00:00.00</date>
<shortText>some short text</shortText>
<important>LOW</important>

</news>

第二个 XML

<?xml version="1.0"?>
<response>
<status>
<code>0</code>
</status>
<newsList>
<news>

<id>1</id>
<text>
Some text here
</text>
</news>

我正在浏览器中转换 XSLT。

结果应该是第一个 XML 中的标题日期和短文本以及第二个 XML 中的文本不正确。

任何线索我如何在 XSLT 中做到这一点。我正在考虑使用该文档。

到目前为止,我在 XSLT 之下。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<h2>News list</h2>
<table border="1">

<xsl:for-each select="newsList/news">
<tr>
  <td><xsl:value-of select="title" /></td>
  <td><xsl:value-of select="shortText" /></td>
   <td><xsl:value-of select="date" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

最后是我用来在浏览器中以 HTML 格式加载转换 XSLT 的脚本。

    <script>
    // the loadXMLDoc function loads the XML and XSL files.
    //It checks what kind of browser the user has and loads the file.

    function loadXMLDoc(dname)
    {
    if (window.XMLHttpRequest)
      {
      xhttp=new XMLHttpRequest();
      }
    else
      {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",dname,false);
    xhttp.send(null);
    xhttp.open
    return xhttp.responseXML;
    }


    //The displayResult() function is used to display the XML file styled by the XSL file.


    function displayNewsOverview(xm_l,xs_l)
    {
    // Load XML and XSL file
    xml=loadXMLDoc(xm_l +".xml");
    xsl=loadXMLDoc(xs_l +".xsl");
    //Test what kind of browser the user has
    // If the user has a browser supporting the ActiveX object (IE)
    if (window.ActiveXObject)
      {
    // Use the transformNode() method to apply the XSL style sheet to the xml document
    // Set the body of the current document (id="news-overview") to contain the styled  xml document

      ex=xml.transformNode(xsl);
      document.getElementById("news-overview").innerHTML=ex;
      }
    // If the user has a browser that does not support the ActiveX object
    else if (document.implementation && document.implementation.createDocument)
      {
    // Create a new XSLTProcessor object and import the XSL file to it
      xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);

    // Use the transformToFragment() method to apply the XSL style sheet to the xml document
      resultDocument = xsltProcessor.transformToFragment(xml,document);

    // Set the body of the current document (id="example") to contain the styled xml document
        document.getElementById("news-overview").appendChild(resultDocument);
      }
    }

    function displayNewsDetails(xm_l,xs_l)
    {
    document.getElementById("news-overview").innerHTML="";

    // Load XML and XSL file
    xml=loadXMLDoc(xm_l +".xml");
    xsl=loadXMLDoc(xs_l +".xsl");

    //Test what kind of browser the user has
    // If the user has a browser supporting the ActiveX object (IE)
    if (window.ActiveXObject)
      {
    // Use the transformNode() method to apply the XSL style sheet to the xml document
    // Set the body of the current document (id="news-overview") to contain the styled xml document

      ex=xml.transformNode(xsl);
      document.getElementById("news-overview").innerHTML=ex;
      }
    // If the user has a browser that does not support the ActiveX object
    else if (document.implementation && document.implementation.createDocument)
      {
    // Create a new XSLTProcessor object and import the XSL file to it
      xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);

    // Use the transformToFragment() method to apply the XSL style sheet to the xml document
      resultDocument = xsltProcessor.transformToFragment(xml,document);

    // Set the body of the current document (id="example") to contain the styled xml document
        document.getElementById("news-overview").appendChild(resultDocument);
      }
    }
    </script>
    </head>


 onLoad="displayNewsOverview('news-overview', 'news-overview')">

任何帮助都会非常感激。是为了一份工作。

谢谢你。

4

1 回答 1

1

您可以在 xslt 中使用 document() 函数 - http://www.w3schools.com/xsl/func_document.asp。您可以尝试添加< ?xml-stylesheet href="nameofxslt" type= "text/xsl" >到您的其中一个 xml 并使用文档功能访问其他的 xml 内容。通过添加 xml 指令 ( <?xml-stylesheet),您将不需要在 javascript 中应用转换。

于 2012-04-18T15:44:55.303 回答