Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 如何使用xslt对文档进行排序?_Xml_Xslt_Xpath - Fatal编程技术网

Xml 如何使用xslt对文档进行排序?

Xml 如何使用xslt对文档进行排序?,xml,xslt,xpath,Xml,Xslt,Xpath,下面是文档_1.xml <products> <product> <name>Pen</name> <Quantity>30</Quantity> </product> <product> <name>Pencil</name> <Quantity>20</Quantit

下面是
文档_1.xml

<products>
    <product>
        <name>Pen</name>
        <Quantity>30</Quantity>
    </product> 
    <product>
        <name>Pencil</name>
        <Quantity>20</Quantity>
    </product>
    <product>
        <name>Bag</name>
        <Quantity>25</Quantity>
    </product>
</products>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/products">
<xsl:copy>
<xsl:apply-templates select="document('document_1.xml')/*/product"/>
<xsl:apply-templates select="document('document_2.xml')/*/product"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()">
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
并且
document.xml

<products>
</products>
我需要输出如下

  • 按数量分类

    <products>
        <product>
            <name>Bag</name>
            <Quantity>2</Quantity>
        </product>
        <product>
            <name>Pencil</name>
            <Quantity>5</Quantity>
        </product>
        <product>
            <name>Pen</name>
            <Quantity>10</Quantity>
        </product>
        <product>
            <name>Pencil</name>
            <Quantity>20</Quantity>
        </product>
        <product>
            <name>Bag</name>
            <Quantity>25</Quantity>
        </product>
        <product>
            <name>Pen</name>
            <Quantity>30</Quantity>
        </product> 
    
    
    纸袋
    2.
    铅笔
    5.
    笔
    10
    铅笔
    20
    纸袋
    25
    笔
    30
    


  • xsl:sort
    标记,但它只能在
    xsl:for each
    循环中使用-例如

    <xsl:for-each select="document('document_1.xml')/*/product|document('document_2.xml')/*/product">
      <xsl:sort select="Quantity" data-type="number" />
      ...
    </xsl:for-each>
    
    
    ...
    
    tks,我可以把它放在我的xsl?xsl:template中。。。如果您对xsl了解不多,请尝试先在更简单的应用程序上学习它,我可以像这样使用这种排序吗?
    <xsl:for-each select="document('document_1.xml')/*/product|document('document_2.xml')/*/product">
      <xsl:sort select="Quantity" data-type="number" />
      ...
    </xsl:for-each>