SO中文参考
首页
(current)
程序语言
c
java
python
c++
go
javascript
swift
c#
操作系统
linux
ubuntu
centos
unix
数据库
oracle
mysql
mongodb
postgresql
框架
node.js
angular
react-native
avalon
django
twisted
hadoop
.net
移动开发
android
ios
搜索
如何使用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
最新问题
如何使用 jQuery 选择属性值包含空格的 HTML 元素? [重复]
用 C 打印图案
p5.js 和 p5.sound.js 不适用于不同的机器
COM 对象中的回调
如何在Windows 10 docker桌面中访问/var/lib/docker?
初学者,Python 类不打印
CodePush 如何处理代码部署以及我的 React Native 应用程序的哪些部分托管在 Microsoft 应用程序中心?
Spark 分桶蜂巢 Metasore
Laravel Auth::attempt 中未定义的数组键“密码”错误
在字符串中查找重复的子字符串[关闭]
如何停止在Python的OpenAI库中生成响应?
CMake:使用compdb自动更新编译数据库
ModuleNotFoundError:Python 中没有名为“models”的模块
InputLayer 应传递“batchInputShape”或“inputShape”
理解 React JSX 中的父元素
抑制 tokio 跟踪订阅者中的外部库日志
removeAndReturnError: 方法不是必须的吗?
Firebase 函数重写正在处理为 GET
用 php 删除 markdown 链接
FormData 不以角度追加文件
© www.soinside.com 2019 - 2024. All rights reserved.