XML 通过 XSLT 转换按内部元素排序的元素
XML element ordering by inner element with XSLT transformation
假设我有以下 XML 并且我想要 XSLT 转换,它会根据 <paymentType>
元素的值更改 <invoice>
元素的顺序。 XSLT 应该是什么样子?
输入:
<invoices>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
<invoice>
<paymentType>
2
</paymentType>
</invoice>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
</invoices>
期望的输出:
<invoices>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
<invoice>
<paymentType>
2
</paymentType>
</invoice>
</invoices>
编辑:
正如建议的那样,我提供了我当前的 XSLT,其中还包括来自该线程中标记答案的解决方案。欢迎提出任何改进建议。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/invoices">
<xsl:copy>
<xsl:apply-templates select="invoice">
<xsl:sort select="paymentType" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="/invoices">
<xsl:copy>
<xsl:apply-templates select="invoice">
<xsl:sort select="paymentType" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
希望你知道其余的。
假设我有以下 XML 并且我想要 XSLT 转换,它会根据 <paymentType>
元素的值更改 <invoice>
元素的顺序。 XSLT 应该是什么样子?
输入:
<invoices>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
<invoice>
<paymentType>
2
</paymentType>
</invoice>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
</invoices>
期望的输出:
<invoices>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
<invoice>
<paymentType>
1
</paymentType>
</invoice>
<invoice>
<paymentType>
2
</paymentType>
</invoice>
</invoices>
编辑: 正如建议的那样,我提供了我当前的 XSLT,其中还包括来自该线程中标记答案的解决方案。欢迎提出任何改进建议。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/invoices">
<xsl:copy>
<xsl:apply-templates select="invoice">
<xsl:sort select="paymentType" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="/invoices">
<xsl:copy>
<xsl:apply-templates select="invoice">
<xsl:sort select="paymentType" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
希望你知道其余的。