3

我想对 xml 元素进行数字排序,但在最后一步失败(合并两个 xml)。
这是我尝试过的:

xml文件的内容

$ cat input.xml
<root>
    <title>hello, world</title>
    <items>
        <item>2</item>
        <item>1</item>
        <item>3</item>
    </items>
</root>

排序项目

$ xmlstarlet sel -R -t -m '//item' -s A:N:- 'number(.)' -c '.' -n input.xml
<xsl-select>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</xsl-select>

删除项目

$ xmlstarlet ed -d '//item' input.xml
<?xml version="1.0"?>
<root>
  <title>hello, world</title>
  <items/>
</root>

如何合并输出?结果应该是:

<root>
    <title>hello, world</title>
    <items>
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </items>
</root>
4

1 回答 1

2

我不熟悉 xmlstarlet,但就我在其文档中看到的内容而言,它可用于将 XSL 转换应用于 XML 文件 ( tr) - 您可以将该命令与此 XSLT 一起使用:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="items">
    <xsl:copy>
      <xsl:apply-templates select="item">
        <xsl:sort select="." data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>

在单个操作中生成排序和合并的输出。

于 2012-04-26T10:43:38.980 回答