有没有办法在 Linux 中将两个 xml 合并为一个 xml
XML 一是这样的:
<dict>
<key>2</key>
<array>
<string>A</string>
<string>B</string>
</array>
<key>3</key>
<array>
<string>C</string>
<string>D</string>
<string>E</string>
</array>
</dict>
XML 二是这样的:
<dict>
<key>A</key>
<array>
<string>A1</string>
<false/>
<false/>
<array>
<string>Apple</string>
<string>This is an apple</string>
</array>
<array>
<string>Apple Pie</string>
<string>I love Apple Pie.</string>
</array>
</array>
<key>B</key>
<array>
<string>B7</string>
<false/>
<false/>
<array>
<string>Boy</string>
<string>I am a boy.</string>
</array>
</array>
</dict>
我想转换成这样:
<dict>
<key>2</key>
<array>
<string>A, Apple, Apple Pie</string>
<string>B, Boy</string>
</array>
...
</dict>
XML one is something like that:
<dict>
<key>2</key>
<array>
<string>A</string>
<string>B</string>
</array>
<key>3</key>
<array>
<string>C</string>
<string>D</string>
<string>E</string>
</array>
</dict>
XML Two is something like that:
<dict>
<key>A</key>
<array>
<string>A1</string>
<false/>
<false/>
<array>
<string>Apple</string>
<string>This is an apple</string>
</array>
<array>
<string>Apple Pie</string>
<string>I love Apple Pie.</string>
</array>
</array>
<key>B</key>
<array>
<string>B7</string>
<false/>
<false/>
<array>
<string>Boy</string>
<string>I am a boy.</string>
</array>
</array>
</dict>
I want to convert to this:
<dict>
<key>2</key>
<array>
<string>A, Apple, Apple Pie</string>
<string>B, Boy</string>
</array>
...
</dict>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 XSLT 来完成此操作,方法是在第一个 XML 文件上应用以下样式表,假设第二个 XML 文件名为
two.xml
:这里的关键技巧(无双关语)是
xsl:key
来索引字符串通过他们的键,以便轻松
并快速查找,并
第二个 XML 文件的上下文节点
在调用之前使用
xsl:for-each
key
功能。编辑。由于您专门询问了有关 Linux 的信息,因此您可以使用
xsltproc
程序将 XSLT 样式表应用到您的输入文件,如下所示:You could do it using XSLT by applying the following stylesheet on the first XML file, assuming the second XML file is named
two.xml
:The key tricks here (no pun intended) are
xsl:key
to index thestrings by their key to allow easy
and quick lookup, and
context node to the second XML file
using
xsl:for-each
before callingthe
key
function.Edit. Since you asked specifically about Linux, you can use the
xsltproc
program to apply the XSLT stylesheet to your input file like this:安装 Java 并使用 XmlMerge。
您可能还会发现 XML 合并很有用。
Install Java and use XmlMerge.
You may also find XML Merger useful.