[Last Call] Learn how to a build a cloud-first strategyRegister Now


XSLT: Merge two XML files (same structure) into one XML with new attributes

Posted on 2005-05-09
Medium Priority
Last Modified: 2013-11-19
I've got two XML files that have the same layout (number of nodes), but different values:

First file (NEW):

    <BBB myAttribute="Y">
        <CCC myAttribute="Y" />

Second file (OLD):

    <BBB myAttribute="N">
        <CCC myAttribute="Y" />

I want to merge the two into one XML file that has attributes from both OLD and NEW files:

    <BBB newAttribute="Y" oldAttribute="N">
        <CCC newAttribute="Y" oldAttribute="Y" />

Could someone provide me with the best way to do this in XSLT.  Keep in mind that the files I'm processing are fairly small (Perhaps up to 15 AAA nodes each with up to 10 BBB nodes) and thus the XSLT method used should be suited to small rather than large files (if relevent).

Also, I'll be using Oracle 9.2's XSLT processor (which can be a bitch) so please keep the solution non-processor specific.  I don't need the PL/SQL code, just the XLS stylesheet.

I'd muddle on through myself, but I'm on an extremely tight deadline and just can't afford to try out all the possible ways to do this.

Thanks very much!
Question by:gadkins
LVL 19

Accepted Solution

ramazanyich earned 2000 total points
ID: 13957760
Let's say you have two files mergeold.xml and mergenew.xml. Then you can use following xsl to produce merged xml. You should apply that xsl to the file mergeold.xml.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml"/>
     <xsl:template match="/">
        <xsl:variable name="new" select="document('mergenew.xml')"/>
          <xsl:apply-templates select="//AAA">
            <xsl:with-param name="newAAA" select="$new//AAA"/>

<xsl:template match="AAA">
<xsl:param name="newAAA"/>
<xsl:variable name="posAAA" select="position()"/>
    <xsl:apply-templates select="BBB">
       <xsl:with-param name="newBBB" select="$newAAA[position()=$posAAA]/BBB"/>

<xsl:template match="BBB">
<xsl:param name="newBBB"/>
<xsl:variable name="posBBB" select="position()"/>
<xsl:attribute name="oldattribute"><xsl:value-of select="@myAttribute"/></xsl:attribute>
<xsl:attribute name="newattribute"><xsl:value-of select="$newBBB[position()=$posBBB]/@myAttribute"/></xsl:attribute>
    <xsl:apply-templates select="CCC">
       <xsl:with-param name="newCCC" select="$newBBB[position()=$posBBB]/CCC"/>


<xsl:template match="CCC">
<xsl:param name="newCCC"/>
<xsl:variable name="posCCC" select="position()"/>
<xsl:attribute name="oldattribute"><xsl:value-of select="@myAttribute"/></xsl:attribute>
<xsl:attribute name="newattribute"><xsl:value-of select="$newCCC[position()=$posCCC]/@myAttribute"/></xsl:attribute>


Author Comment

ID: 13965027
Thanks very much!  Worked a treat.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question