如何合并两个 XML 文件 [英] How to merge two XML files

查看:149
本文介绍了如何合并两个 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个xml文件,需要合并成一个xml.例子如下:

I have two xml files, which need to be merged into one xml. Here is the example:

原始.xml文件:

<employees>
  <employee id="1">
    <name> Name1 </name>
    <email> email1 <email>
  </employee>
  <employee id="2">
    <name> Name2 </name>
    <email> email2 <email>
  </employee>
</employees>

update.xml 文件:

update.xml file:

<employees>
        <employee id="2">
            <name> Name2 </name>
            <email> email_New <email>
        </employee>
        <employee id="3">
            <name> Name3 </name>
            <email> email3 <email>
        </employee>
</employees>

它们应该像这样合并到一个 xml 文件中:

they should be merged to a xml file like this:

<employees>
  <employee id="1">
    <name> Name1 </name>
    <email> email1 <email>
  </employee>  
  <employee id="2">
    <name> Name2 </name>
    <email> email_New <email>
  </employee>
  <employee id="3">
    <name> Name3 </name>
    <email> email3 <email>
  </employee>
</employees>

实际上,我想使用 update.xml 来更新 orginal.xml :

Actually, I'd like to use the update.xml to update the orginal.xml :

  1. update.xml 中的新员工应添加到 original.xml

  1. new employee in update.xml should be added to original.xml

update.xml 中修改的员工信息应该覆盖相应的员工节点.

modified employee information in update.xml should overwrite the corresponding employee node.

我对 XSLT 有一点了解,但我的知识不足以找出适合合并的 XSLT.

I know a little of XSLT, but my knowledge is not enough to figure out the right XSLT for the merge.

推荐答案

请试一试:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="fileName" select="'update.xml'" />
  <xsl:param name="updates" select="document($fileName)" />

  <xsl:variable name="updateEmployees" select="$updates/employees/employee" />

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

  <xsl:template match="employees">
    <xsl:copy>
      <xsl:apply-templates select="employee[not(@id = $updateEmployees/@id)]" />
      <xsl:apply-templates select="$updateEmployees" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

当使用您的第一个 XML 作为输入并且 update.xml 存在于同一文件夹中时,这会产生:

When run with your first XML as the input and with the update.xml present in the same folder, this produces:

<employees>
  <employee id="1">
    <name> Name1 </name>
    <email>
      email1 </email>
  </employee>
  <employee id="2">
    <name> Name2 </name>
    <email>
      email_New
    </email>
  </employee>
  <employee id="3">
    <name> Name3 </name>
    <email>
      email3
    </email>
  </employee>
</employees>

fileNameupdates 被声明为参数,因此如果更新位于名为update.xml"以外的文件中,或者您想要传递更新 XML直接使用,您无需对 XSLT 进行任何修改即可完成此操作.

fileName and updates are declared as parameters, so if the updates are in a file named something other than "update.xml" or you want to pass the update XML in directly, you can do that without making any modifications to the XSLT.

这篇关于如何合并两个 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆