如何使用XSLT按ID值对XML节点排序?
我有一个包含许多行节点的大型xml文件。我想按每行的id值对其进行排序 这将是一个示例输入:如何使用XSLT按ID值对XML节点排序?,xml,sorting,xslt,xmlstarlet,Xml,Sorting,Xslt,Xmlstarlet,我有一个包含许多行节点的大型xml文件。我想按每行的id值对其进行排序 这将是一个示例输入: <database> <table> <row> <id>10</id> <foo>bar</foo> </row> <row> <id>5</id> <foo>poit</foo
<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>
但它失败了:
$ 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
参数数无效
XmlXPathCompiledVal:计算失败
参数数无效
XmlXPathCompiledVal:计算失败
参数数无效
XmlXPathCompiledVal:计算失败
我对xmlstarlet一无所知,但我可以说您的XSLT应该是这样的
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="table">
<xsl:copy>
<xsl:apply-templates select="row">
<xsl:sort select="id" data-type="number" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
请注意,在XSLT中,元素名称之后不需要()
看看它在哪里起作用
(我还注意到标记
xmlstarlet
只有20个追随者。您可能想尝试其他一些工具。请参阅以获取帮助。可能是xsltproc?xmlstarlet工作正常:xmlstarlet tr sort.xsl example.xml | xmlstarlet fo-
)谢谢,这是我需要的kickstart。旁注:xmlstarlet希望这是一个更大文件的1.0版。我的样式表实际上是“1.0”版本,我只是忘了更改版本号。如果给XSLT1.0进程一个版本为“2.0”的样式表,那么它将在“前向兼容性”模式下运行,并将忽略任何它无法识别的xsl指令。
$ 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>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="table">
<xsl:copy>
<xsl:apply-templates select="row">
<xsl:sort select="id" data-type="number" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>