Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Merge two xml files with XSLT

Tags:

xml

xslt

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

First XML is (the original one):

<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:

   <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 :

 <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.

like image 851
skyfree Avatar asked Mar 04 '13 04:03

skyfree


1 Answers

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

<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:

<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>
like image 100
JLRishe Avatar answered Oct 28 '22 13:10

JLRishe