我有以下XML,我想在City上进行排序。
<catalog>
<cd>
<City>New York</City>
<City>Mumbai</City>
<City>Kolkata</City>
<City>Paris</City>
<City>London</City>
<City>Sydney</City>
<City>California</City>
<City>Vizag</City>
<City>Uganda</City>
</cd>
</catalog>
我尝试在XSLT下面进行排序,但是它只考虑第一个City元素并只给出一个城市,但我希望所有城市都按照排序方式(升序/降序)。
<?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="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>City</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="City"/>
<tr>
<td><xsl:value-of select="City"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
提前致谢!!!
答案 0 :(得分:2)
这应该这样做:
<xsl:for-each select="catalog/cd/City">
<xsl:sort select="."/>
<tr>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
然而,使用模板会更优雅:
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>City</th>
</tr>
<xsl:apply-templates select="catalog/cd/City">
<xsl:sort select="."/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="City">
<tr>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>