2012-10-24 31 views
1

XML輸入:XML排序FN不工作如下

<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> 

我想輸出返回產品由升屬性名稱但我的排序不起作用,因爲它仍然顯示產品球,然後只有娃娃。請告訴如何使其工作。

+0

您正在存儲ProductToy元素,但只有一個ProductToy元件;僅使用一個元素對一個集合進行排序不是一個非常有用的操作。此外,您正在按名稱屬性的值排序它們,但您的ProductToy元素和其他任何內容都沒有名稱屬性。 –

回答

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>