我有一个包含许多行节点的大型XML文件。我想按每行的ID值对它进行排序。
以下是一个示例输入:
这是期望的输出:
如何实现这一点?我可以使用
我以前没有使用过xslt,不确定该怎么做。
我找到了一些相关的排序问题,提供了一些XSLT示例,但我无法在我的用例中让它们正常工作。
我的当前
然而它失败了:
以下是一个示例输入:
<database>
<table>
<row>
<id>10</id>
<foo>bar</foo>
</row>
<row>
<id>5</id>
<foo>poit</foo>
</row>
<row>
<id>1</id>
<foo>narf</foo>
</row>
</table>
</database>
这是期望的输出:
<database>
<table>
<row>
<id>1</id>
<foo>narf</foo>
</row>
<row>
<id>5</id>
<foo>poit</foo>
</row>
<row>
<id>10</id>
<foo>bar</foo>
</row>
</table>
</database>
如何实现这一点?我可以使用
xmlstarlet
。它具有转换功能,显然我可以在xsl文件中提供xslt样式表。我以前没有使用过xslt,不确定该怎么做。
我找到了一些相关的排序问题,提供了一些XSLT示例,但我无法在我的用例中让它们正常工作。
我的当前
sort.xslt
(注意:我不知道自己在做什么)如下:<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*">
<xsl:sort select="row()"/>
</xsl:apply-templates>
<xsl:apply-templates select="node()">
<xsl:sort select="id()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
然而它失败了:
$ xmlstarlet tr sort.xsl example.xml
Invalid number of arguments
xmlXPathCompiledEval: evaluation failed
Invalid number of arguments
xmlXPathCompiledEval: evaluation failed
Invalid number of arguments
xmlXPathCompiledEval: evaluation failed
<database>
<table/>
</database>
xmlstarlet tr sort.xsl example.xml | xmlstarlet fo -
;) 谢谢,这是我需要的启动。 - k0pernikus