如何使用xmlstarlet合并两个xml?你好,世界</</desc> <question vote="3"> <p>我想对 xml 元素进行数字排序,但在最后一步(合并两个 xml)失败了。<br/> 这是我试过的:</p> <h3>xml文件的内容</h3> <pre><code>$ cat input.xml <root> <title>hello, world</title> <items> <item>2</item> <item>1</item> <item>3</item> </items> </root> </code></pre> <h3>排序项目</h3> <pre><code>$ 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> </code></pre> <h3>删除项目</h3> <pre><code>$ xmlstarlet ed -d '//item' input.xml <?xml version="1.0"?> <root> <title>hello, world</title> <items/> </root> </code></pre> <h3>如何合并输出?结果应该是:</h3> <pre><code><root> <title>hello, world</title> <items> <item>1</item> <item>2</item> <item>3</item> </items> </root> </code></pre> </question> <answer tick="false" vote="2"> <p>我不熟悉 xmlstarlet,但就我在其文档中看到的内容而言,它可用于将 XSL 转换应用于 XML 文件 (<pre><code>tr</code></pre>) - 您可以将此命令与此 XSLT 一起使用:</p> <pre><code><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> </code></pre> <p>在单个操作中生成排序和合并的输出。</p> </answer> <answer tick="false" vote="0"> <p>你已经问了一段时间了,但仍然如此。下壳 脚本使用多阶段流水线产生想要的结果—— 尽管这不是处理更大输入的最佳方式。 有关使用 <a href="http://www.w3.org/TR/xinclude/" rel="nofollow noreferrer" title="W3C XML Inclusions 1.0 2E recommendation">XInclude</a> 和 <pre><code>xmlstarlet</code></pre> 的方法,请参见 <a href="https://stackoverflow.com/a/76035373/14403369">这个</a>答案。</p> <pre><code># shellcheck shell=sh xmlstarlet select -R -t -m '//item' -s 'A:N:-' '.' -c '.' input.xml | xmlstarlet select -R -t -c '/ | document("-")' input.xml | xmlstarlet edit \ -d '/xsl-select/root//item' \ -m '/xsl-select/xsl-select/item' '/xsl-select/root/items' | xmlstarlet select -B -I -t -c '/xsl-select/*[1]' </code></pre> <ol> <li>运行<pre><code>select</code></pre>提取<pre><code>item</code></pre>s的排序文件(到<pre><code>stdout</code></pre>)</li> <li>运行<pre><code>select</code></pre>复制原始输入和排序文件和 (<pre><code>-R</code></pre>) 使用 XSLT 将它们包装在一起 <a href="https://www.w3.org/TR/1999/REC-xslt-19991116#function-document" rel="nofollow noreferrer" title="node-set document(object, node-set?) - W3C XSLT 1.0 recommendation"><pre><code>document</code></pre></a>功能访问 <pre><code>stdin</code></pre> 上的排序文件(下面列出了此步骤的输出)</li> <li>调用<pre><code>edit</code></pre>删除未排序的<pre><code>item</code></pre>s并移动已排序的 <pre><code>item</code></pre>就位</li> <li>运行<pre><code>select</code></pre>提取并格式化合并后的文档</li> </ol> <p>第 2 步的输出(缩进):</p> <pre><code><xsl-select> <root> <title>hello, world</title> <items> <item>2</item> <item>1</item> <item>3</item> </items> </root> <xsl-select> <item>1</item> <item>2</item> <item>3</item> </xsl-select> </xsl-select> </code></pre> </answer> </body></html>

问题描述 投票:0回答:0
xml sorting xpath xmlstarlet
© www.soinside.com 2019 - 2024. All rights reserved.