XSLT排序没有for-each

时间:2013-06-14 11:18:06

标签: xml sorting xslt

我有练习在不使用“for-each”的情况下对xml文件进行排序。 使用我的脚本,我得到了托管,但它们没有排序。

Xml文件: http://www.w3schools.com/xml/cd_catalog.xml

<xsl:stylesheet version = '1.0'
 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="CD">
<br/>
<xsl:apply-templates select="TITLE" >
  <xsl:sort select="TITLE"/>
</xsl:apply-templates>

</xsl:template>

THX

2 个答案:

答案 0 :(得分:1)

因为CD中只有一个TITLE,所以我假设您想要通过TITLE对CD进行排序: 尝试类似:

<xsl:stylesheet version = '1.0'
 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="CATALOG">
   <xsl:apply-templates select="CD" >
     <xsl:sort select="TITLE"/>
   </xsl:apply-templates>
 </xsl:template>

<xsl:template match="CD">
  <xsl:value-of select="TITLE" />
  <br/>
</xsl:template>

 </xsl:stylesheet> 

答案 1 :(得分:-1)

您的模板有点不对...首先,您没有根匹配模板,其次,您应该迭代TITLE,而您应该遍历CD

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

<xsl:template match="/">
    <xsl:apply-templates select="CD" >
        <xsl:sort select="TITLE"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="CD">
    <xsl:value-of select="TITLE" />
</xsl:template>

您正在查看的输出可能不是由您的模板生成的,而是由应用于/的默认模板生成的。