2017-07-18 38 views
0

我需要顯示兩個嵌套的FOR-EACH的結果,以字母順序顯示兩個嵌套的FOR-EACH的結果。我嘗試使用節點集但它不起作用。 如何排序For-Each的所有結果?XSL嵌套SORT問題

數據:

<?xml version="1.0" encoding="UTF-8"?> 
<list> 
<catalog> 
    <cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
    </cd> 
    <cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
    </cd> 
    <cd> 
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
    </cd> 

</catalog> 
<catalog> 
<cd> 
    <title>When a man loves a woman</title> 
    <artist>Zercy Sledge</artist> 
    <country>USA</country> 
    <company>Atlantic</company> 
    <price>8.70</price> 
    <year>1987</year> 
    </cd> 
    <cd> 
    <title>Black angel</title> 
    <artist>Avage Rose</artist> 
    <country>EU</country> 
    <company>Mega</company> 
    <price>10.90</price> 
    <year>1995</year> 
    </cd> 
    <cd> 
    <title>1999 Grammy Nominees</title> 
    <artist>Iany</artist> 
    <country>USA</country> 
    <company>Grammy</company> 
    <price>10.20</price> 
    <year>1999</year> 
    </cd> 

</catalog> 

</list> 

代碼:

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

<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Artist</th> 
    </tr> 
    <xsl:for-each select="list/catalog"> 
<xsl:for-each select="cd"> 
     <xsl:sort select="artist"/> 
    <tr> 
     <td><xsl:value-of select="title"/></td> 
    <td><xsl:value-of select="artist"/></td> 
    </tr> 
    </xsl:for-each> 
</xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

結果: Result: 預計: Expected:

回答

1

您單獨排序每個catalog。要排序的所有cd s ^在一起,無論他們catalog的,做簡單:

<xsl:template match="/"> 
    <html> 
     <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
       <th>Title</th> 
       <th>Artist</th> 
      </tr> 
      <xsl:for-each select="list/catalog/cd"> 
       <xsl:sort select="artist"/> 
       <tr> 
        <td><xsl:value-of select="title"/></td> 
        <td><xsl:value-of select="artist"/></td> 
       </tr> 
      </xsl:for-each> 
     </table> 
     </body> 
    </html> 
</xsl:template> 
+0

嗨米歇爾, 我明白你的答案,但我需要的,因爲我使用的目錄外的其他節點來獲得其他變量分別做。這只是一個例子,但我需要爲每個單獨做。是否可以在for-eachs之後排序? –

+0

恐怕我不明白你的評論。如果你將每個目錄分開,那麼你會得到你原來的結果 - 你不想要的。請編輯你的問題並提供一個更完整的例子,包括那些你說爲你創建問題的「其他節點」。 –