Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xml排序fn不工作_Xml_Xpath - Fatal编程技术网

xml排序fn不工作

xml排序fn不工作,xml,xpath,Xml,Xpath,XML输入如下所示: <Global> <ProductFood> <foodName>Burger</foodName> <foodName>Snack</foodName> </ProductFood> <ProductToy> <Product ProductID="1"> <

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> 

我希望输出通过升序返回产品属性名称。但我的排序不起作用,因为它仍然先显示产品球,然后仅显示玩偶。请建议如何使其工作。

使用:

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

2.
1.

您正在存储ProductToy元素,但只有一个ProductToy元素;仅使用一个元素对集合进行排序不是一个非常有用的操作。此外,您正在按照name属性的值对它们进行排序,但是ProductToy元素和其他任何元素都没有name属性。