Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5941451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:06:44+00:00 2026-05-22T16:06:44+00:00

I want to compare two xmls and then merge them. For example: myFile1.xml <?xml

  • 0

I want to compare two xmls and then merge them. For example:

myFile1.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
</data>
<data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
</data>
</catalog>

myFile2.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<data>
    <title>Title1</title>
    <description>Description1</description>
    <author>Author1</author>
    <date>12/34/5678</date>
    <myid>1</myid>
</data>
<data>
    <author>Author2</author>
    <date>87/65/4321</date>
    <myid>2</myid>
</data>
</catalog>

Desired Output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
    <author>Author1</author>
    <date>12/34/5678</date>
</data>
<data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
    <author>Author2</author>
    <date>87/65/4321</date>
</data>
</catalog>

I have a code but, it doesnot perform as per the required output.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:variable name="compare" select="'myFile1.xml'"/>
<xsl:variable name="with" select="'myFile2.xml'"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:variable name="info1" select="document($compare)/catalog/data[myid=current()/myid]/."/>
        <xsl:variable name="info2" select="document($with)/catalog/data[myid=current()/myid]/."/>
        <xsl:for-each select="$info1/*">
            <xsl:variable name="check1" select="name(current())"/>
            <!--xsl:text>Current node1 : </xsl:text><xsl:value-of select="$check1"/-->
            <xsl:for-each select="$info2/*">
                <xsl:variable name="check2" select="name(current())"/>
                <!--xsl:text>Current node2 : </xsl:text><xsl:value-of select="$check2"/-->
                <xsl:if test="$check1!=$check2">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
</xsl:transform>

Please Help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:06:44+00:00Added an answer on May 22, 2026 at 4:06 pm

    This solution is totally free of loops or keys. I’ve loaded only one document using document(), while I use the other one as source. Briefly, an element missing in the source document, it is taken on the loaded one. More elements you have less usable is this solution. See bottom for a more general one.


    XSLT 1.0 tested on Saxon-HE 9.2.1.1J

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:variable name="catalog2" select="document('source_test2.xml')/catalog"/>
    
        <xsl:template match="catalog">
            <catalog>
                <xsl:apply-templates select="data"/>
            </catalog>
        </xsl:template>
    
        <xsl:template match="data">
            <xsl:variable name="data2" select="$catalog2/data[myid=current()/myid]/."/>
            <data>
                <xsl:choose>
                    <xsl:when test="title">
                        <xsl:copy-of select="title"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="$data2/title"/>
                    </xsl:otherwise>
                </xsl:choose>
    
                <xsl:choose>
                    <xsl:when test="description">
                        <xsl:copy-of select="description"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="$data2/description"/>
                    </xsl:otherwise>
                </xsl:choose>
    
                <xsl:copy-of select="myid"/>
    
                <xsl:choose>
                    <xsl:when test="author">
                        <xsl:copy-of select="author"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="$data2/author"/>
                    </xsl:otherwise>
                </xsl:choose>
    
                <xsl:choose>
                    <xsl:when test="date">
                        <xsl:copy-of select="date"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="$data2/date"/>
                    </xsl:otherwise>
                </xsl:choose>
    
            </data>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Here follows a more general solution. The approach is the same. For each data, an element present in myFile2 and missing in myFile1 is added to the result tree, and vice-versa.

    XSLT 1.0 tested on Saxon-B 9.0.0.4J

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:variable name="catalog2" select="document('myFile2.xml')/catalog"/>
    
        <xsl:template match="catalog">
            <catalog>
                <xsl:apply-templates select="data"/>
            </catalog>
        </xsl:template>
    
        <xsl:template match="data">
            <xsl:variable name="data1" select="."/>
            <xsl:variable name="data2" select="$catalog2/data[myid=current()/myid]/."/>
            <data>
                <xsl:copy-of select="$data1/*"/>
                <xsl:for-each select="$data2/*">
                    <xsl:variable name="element2" select="name(.)"/>
                    <xsl:if test="count($data1/*[name()=$element2])=0">
                        <xsl:copy-of select="."/>
                    </xsl:if>
                </xsl:for-each>
            </data>
        </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compare two arrays and then order one of them based on
I want to compare two xml raw files and capture their differences in data.
I'm trying to compare two Xml files using C# code. I want to ignore
I want to compare two tabdelimeted files. I take the files and converts them
I want to compare two strings and one of them may be a null
I want to compare two xml strings in a test, but the test keeps
I want to compare two binary files. One of them is already stored on
I want to compare two ms-access .mdb files to check that the data they
I want to compare two CSV files to see if they are different. I
I want to compare two times in VB.net: I have 1:42:21 PM and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.