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

Posted on 2005-05-09
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

    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>

    LVL 2

    Author Comment

    Thanks very much!  Worked a treat.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System ( introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now