9

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.

skyfree
  • 867
  • 2
  • 10
  • 29
  • Tough in XSLT1 without using extensions; trivial in XSLT2. Which XSLT processor are you using, and does it support XSLT2? – Jim Garrison Mar 04 '13 at 04:57
  • Hi Jim, xslt1 is used in my project. thanks. – skyfree Mar 04 '13 at 05:01
  • 1
    When you searched stackoverflow did you perhaps find [your own question that was answered yesterday](http://stackoverflow.com/q/15175287/1945651)? – JLRishe Mar 04 '13 at 07:15
  • @JLRishe yes, I asked similar question yesterday, but this one is a slight different from the former one. And actually each xml files has a header part which also need to be merged. I am really new on XSLT, Please help, thanks. – skyfree Mar 04 '13 at 10:19
  • If that's the key difference then I think it would have made sense to include that when you posted this new question instead of 5 hours later. :) – JLRishe Mar 04 '13 at 11:02

1 Answers1

16

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>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thank you , you are a life savior! How there is still a bit problem. actually, there are a header before all the entries which need to be merged too.. I have modified the original quesiton . – skyfree Mar 04 '13 at 09:36
  • JLRishe, @JLRishe Your solution works great expcept the header part. sorry for my incomplete description, I have tried to figure it out, but didn't work. please help. Thanks a lot. – skyfree Mar 04 '13 at 09:47
  • I've updated my answer above. Do you need to also merge the header values, or just use the ones from the original XML? – JLRishe Mar 04 '13 at 11:01
  • 1
    @Lijo This question has nothing to do with C# so I'm not going to edit my answer to provide you C# code, but hopefully this helps: https://www.google.co.jp/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23+xslt – JLRishe Feb 28 '17 at 07:08