11

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>
4
  • Tough in XSLT1 without using extensions; trivial in XSLT2. Which XSLT processor are you using, and does it support XSLT2? Commented Mar 4, 2013 at 4:57
  • 1
    When you searched stackoverflow did you perhaps find your own question that was answered yesterday? Commented Mar 4, 2013 at 7: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. Commented Mar 4, 2013 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. :) Commented Mar 4, 2013 at 11:02

1 Answer 1

19

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>
Sign up to request clarification or add additional context in comments.

4 Comments

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 .
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.
I've updated my answer above. Do you need to also merge the header values, or just use the ones from the original XML?
@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: google.co.jp/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.