2017-07-18 57 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

恐怕我不明白你的评论。如果你将每个目录分开,那么你会得到你原来的结果 - 你不想要的。请编辑你的问题并提供一个更完整的例子,包括那些你说为你创建问题的“其他节点”。 –