-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>

2 番目の XML

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

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

ブラウザで XSLT を変換しています。

結果は、最初の XML からのタイトルの日付と短いテキスト、および 2 番目の 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>

最後に、変換 XSLT を HTML 形式でブラウザにロードするために使用しているスクリプトです。

    <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 の 1 つに追加< ?xml-stylesheet href="nameofxslt" type= "text/xsl" >して、ドキュメント機能を使用して、他の xml コンテンツにアクセスすることができます。xml ディレクティブ ( <?xml-stylesheet) を追加すると、javascript で変換を適用する必要がなくなります。

于 2012-04-18T15:44:55.303 に答える