xml排序fn不起作用

时间:2012-10-24 05:03:36

标签: xml xpath

XML输入如下:

  <Global>
    <ProductFood>
        <foodName>Burger</foodName>
        <foodName>Snack</foodName>
    </ProductFood>
    <ProductToy>     
      <Product ProductID="1"> 
         <productName>Doll</productName> 
         <Color>Green</Color> 
      </Product> 
      <Product ProductID="2"> 
         <productName>Ball</productName> 
         <Color>White</Color> 
      </Product>       
    </ProductToy>
 </Global>

我拥有的XSLT代码如下:

<xsl:template match="//Products"> 
    <html> 
        <body> 
            <Products> 
                <xsl:apply-templates select="ProductToy" > 
                <xsl:sort select="@name"/>
                <xsl:apply-templates/> 
            </Products> 
        </body> 
    </html> 
</xsl:template> 

<xsl:template match="Product"> 
    <xsl:element name="product"> 
        <xsl:attribute name="name" select="ProductName/text()" /> 
        <xsl:element name="productID"> 
            <xsl:value-of select="@ProductID" /> 
        </xsl:element> 
    </xsl:element> 
</xsl:template> 

我希望输出按升序返回产品属性名称。但我的排序不起作用,因为它仍然首先显示产品球然后只有娃娃。请告知如何使其工作。

1 个答案:

答案 0 :(得分:0)

使用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/Global">
    <html>
      <body>
        <Products>
          <xsl:apply-templates select="//Product">
            <xsl:sort select="productName"/>
          </xsl:apply-templates>
        </Products>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Product">
    <product name="{productName}">
      <productID>
        <xsl:value-of select="@ProductID"/>
      </productID>
    </product>
  </xsl:template>
</xsl:stylesheet>

输出:

<html>
  <body>
    <Products>
      <product name="Ball">
        <productID>2</productID>
      </product>
      <product name="Doll">
        <productID>1</productID>
      </product>
    </Products>
  </body>
</html>
相关问题