如何使用 XSLT 合并两个 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15194718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:54:25  来源:igfitidea点击:

how to Merge two xml files with XSLT

xmlxslt

提问by skyfree

I have two xml files which need to be merged into one by using XSLT.

我有两个需要使用 XSLT 合并为一个的 xml 文件。

First XML is (the original one):

第一个 XML 是(原始的):

<feed>
  <author> 
    <firstName>f</firstName>
    <lastName>l</lastName>
  </author>
  <date>2011-01-02 </date>
  <entry>
    <id>1</id>
    <Name>aaa</Name>
    <Content>XXX</Content>     
  </entry>
  <entry>
    <id>2</id>
    <Name>bbb</Name>
    <Content>YYY</Content>   
  </entry>
</feed>

Second XML(updated data) is like this:

第二个 XML(更新数据)是这样的:

   <feed>
      <author> 
       <firstName>f</firstName>
       <lastName>l</lastName>
      </author>
      <date>2012-05-02 </date>
      <entry>
        <id>2</id>
        <Name>newName</Name>
        <Content>newContent</Content>     
      </entry>
      <entry>
        <id>3</id>
        <Name>ccc</Name>
        <Content>ZZZ</Content>   
      </entry>
  </feed>

The desired merged result - using the second XML to update the first one :

所需的合并结果 - 使用第二个 XML 更新第一个:

 <feed>
      <author> 
       <firstName>f</firstName>
       <lastName>l</lastName>
      </author>
      <date>2012-05-02 </date>
      <entry>
        <id>1</id>
        <Name>aaa</Name>
        <Content>XXX</Content>     
      </entry>     
      <entry>
        <id>2</id>
        <Name>newName</Name>
        <Content>newContent</Content>     
      </entry>
      <entry>
        <id>3</id>
        <Name>ccc</Name>
        <Content>ZZZ</Content>   
      </entry>
   </feed>

I have searched stackoverflow but still could not find the answer. Thanks for help.

我已经搜索过stackoverflow,但仍然找不到答案。感谢帮助。

回答by JLRishe

Pretty much the same answer as I provided to your last question, modified to match your new XML format:

与我为您的上一个问题提供的答案几乎相同,经过修改以匹配您的新 XML 格式:

<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="'updates.xml'" />
  <xsl:param name="updates" select="document($fileName)" />

  <xsl:variable name="updateItems" select="$updates/feed/entry" />

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

  <xsl:template match="feed">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()[not(self::entry)] | 
                                   entry[not(id = $updateItems/id)]" />
      <xsl:apply-templates select="$updateItems" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

When run on the first sample XML, with the second one saved as "updates.xml", this produces:

当在第一个示例 XML 上运行时,将第二个保存为“updates.xml”,这会产生:

<feed>
  <author>
    <firstName>f</firstName>
    <lastName>l</lastName>
  </author>
  <date>2011-01-02 </date>
  <entry>
    <id>1</id>
    <Name>aaa</Name>
    <Content>XXX</Content>
  </entry>
  <entry>
    <id>2</id>
    <Name>newName</Name>
    <Content>newContent</Content>
  </entry>
  <entry>
    <id>3</id>
    <Name>ccc</Name>
    <Content>ZZZ</Content>
  </entry>
</feed>