有没有办法在 Linux 中将两个 xml 合并为一个 xml

发布于 2024-09-01 14:27:38 字数 1473 浏览 7 评论 0原文

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-09-08 14:27:38

您可以使用 XSLT 来完成此操作,方法是在第一个 XML 文件上应用以下样式表,假设第二个 XML 文件名为 two.xml

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="string-by-key"
           match="/dict/array/array/string[1]"
           use="preceding::key[1]"/>
  <xsl:template match="dict">
    <dict>
      <xsl:apply-templates select="key"/>
    </dict>
  </xsl:template>
  <xsl:template match="key">
    <key>
      <xsl:value-of select="."/>
    </key>
    <array>
      <xsl:apply-templates select="following-sibling::array[1]/string"/>
    </array>
  </xsl:template>
  <xsl:template match="string">
    <string>
      <xsl:variable name="key" select="."/>
      <xsl:value-of select="$key"/>
      <xsl:for-each select="document('two.xml')">
        <xsl:for-each select="key('string-by-key', $key)">
          <xsl:text>, </xsl:text>
          <xsl:value-of select="."/>
        </xsl:for-each>
      </xsl:for-each>
    </string>
  </xsl:template>
</xsl:stylesheet>

这里的关键技巧(无双关语)是

  1. 使用 xsl:key 来索引
    字符串通过他们的键,以便轻松
    并快速查找,并
  2. 更改
    第二个 XML 文件的上下文节点
    在调用之前使用 xsl:for-each
    key 功能。

编辑。由于您专门询问了有关 Linux 的信息,因此您可以使用 xsltproc 程序将 XSLT 样式表应用到您的输入文件,如下所示:

xsltproc stylesheet.xsl one.xml

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:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="string-by-key"
           match="/dict/array/array/string[1]"
           use="preceding::key[1]"/>
  <xsl:template match="dict">
    <dict>
      <xsl:apply-templates select="key"/>
    </dict>
  </xsl:template>
  <xsl:template match="key">
    <key>
      <xsl:value-of select="."/>
    </key>
    <array>
      <xsl:apply-templates select="following-sibling::array[1]/string"/>
    </array>
  </xsl:template>
  <xsl:template match="string">
    <string>
      <xsl:variable name="key" select="."/>
      <xsl:value-of select="$key"/>
      <xsl:for-each select="document('two.xml')">
        <xsl:for-each select="key('string-by-key', $key)">
          <xsl:text>, </xsl:text>
          <xsl:value-of select="."/>
        </xsl:for-each>
      </xsl:for-each>
    </string>
  </xsl:template>
</xsl:stylesheet>

The key tricks here (no pun intended) are

  1. the use of xsl:key to index the
    strings by their key to allow easy
    and quick lookup, and
  2. changing the
    context node to the second XML file
    using xsl:for-each before calling
    the 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:

xsltproc stylesheet.xsl one.xml
多像笑话 2024-09-08 14:27:38

安装 Java 并使用 XmlMerge

您可能还会发现 XML 合并很有用。

Install Java and use XmlMerge.

You may also find XML Merger useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文