2012-04-19 81 views
0

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

結果應該dysplaing標題日期從第一個XML簡短文本,並從第二XML文本。

在XSLT下面我到目前爲止。

<xsl:template match="response"> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <th align="left">Title</th> 
     <th align="left">shortText</th> 
     <th align="left">date</th> 
     <th align="left">text</th> 
    </tr> 
    <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> 
     <td><xsl:value-of select="document('news-details.xml')//news[id=$id_news]/text"/> 
    </td> 
    </tr> 
    </xsl:for-each> 
    </table> 
</xsl:template> 
</xsl:stylesheet> 

但是,這將始終顯示從消息編號1

文本我知道豈不等於無法更新vaulue但如何能做到這件事?

回答

0
<xsl:template match="response"> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th align="left">Title</th> 
     <th align="left">shortText</th> 
     <th align="left">date</th> 
     <th align="left">text</th> 
     </tr> 
     <xsl:apply-templates select="newsList/news"/> 
    </table> 
    </xsl:template> 

    <xsl:template match="newsList/news"> 
    <xsl:variable name="id_news" select="ID"/> 
    <tr> 
     <td><xsl:value-of select="title" /></td> 
     <td><xsl:value-of select="shortText" /></td> 
     <td><xsl:value-of select="date" /></td> 
     <td> 
     <xsl:apply-templates select="document('news-details.xml')//news[id=$id_news]/text"/> 
     </td> 
    </tr> 
    </xsl:template> 

    <xsl:template match="text"> 
    <xsl:value-of select="."/> 
    </xsl:template> 
1

下面是一個例子使用的關鍵:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output method="html" version="5.0"/> 

    <xsl:param name="url2" select="'news-details.xml'"/> 
    <xsl:variable name="doc2" select="document($url2, /)"/> 

    <xsl:key name="k1" match="news" use="id"/> 

    <xsl:template match="/"> 
    <html> 
     <head> 
     <title>Example</title> 
     </head> 
     <body> 
     <table> 
      <thead> 
      <tr> 
       <th>Title</th> 
       <th>short text</th> 
       <th>date</th> 
       <th>text</th> 
      </tr> 
      </thead> 
      <tbody> 
      <xsl:apply-templates select="//news"/> 
      </tbody> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="news"> 
    <xsl:variable name="id" select="id"/> 
    <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="shortText"/></td> 
     <td><xsl:value-of select="date"/></td> 
     <td> 
     <xsl:for-each select="$doc2"> 
      <xsl:value-of select="key('k1', $id)/text"/> 
     </xsl:for-each> 
     </td> 
    </tr> 
    </xsl:template> 

</xsl:stylesheet> 
+0

謝謝,它的工作!太好了,我沒有考慮使用鑰匙! – user1341765 2012-04-19 10:28:36