我有这两个文件:
XML
<?xml version="1.0" encoding="UTF-8"?>
<Store>
<Plant id="10">
<Common>Pianta carnivora</Common>
<Botanical>Dionaea muscipula</Botanical>
<Quantity>10</Quantity>
</Plant>
<Flower id="3">
<Common>Fiore di prova</Common>
<Quantity>999</Quantity>
</Flower>
<Plant id="20">
<Common>Canapa</Common>
<Botanical>Cannabis</Botanical>
<Quantity>2</Quantity>
</Plant>
<Plant id="30">
<Common>Loto</Common>
<Botanical>Nelumbo Adans</Botanical>
<Quantity>3</Quantity>
</Plant>
</Store>
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="Store">
<body>
<xsl:for-each select="Plant">
<p>
<xsl:sort select="Quantity"/>
<xsl:value-of select="Quantity"/>
</p>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>
XSL没有排序。我没有任何输出。我真的不知道它是如何起作用的。代码似乎是正确的。如果取下sort标签,您将看到输出。在排序中,你什么都看不到。
答案 0 :(得分:1)
您需要将xsl:sort
移至xsl:for-each
的第一个孩子。它在现在的位置无效。
您可能还想将data-type
更改为number
。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="Store">
<body>
<xsl:for-each select="Plant">
<xsl:sort select="Quantity" data-type="number"/>
<p>
<xsl:value-of select="Quantity"/>
</p>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>
您也可以使用xsl:apply-templates
执行相同类型的操作...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Store">
<html>
<body>
<xsl:apply-templates select="Plant/Quantity">
<xsl:sort data-type="number"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="Quantity">
<p><xsl:value-of select="."/></p>
</xsl:template>
</xsl:stylesheet>