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 6026945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:31:08+00:00 2026-05-23T04:31:08+00:00

I need to find a way to join two XML files when they have

  • 0

I need to find a way to join two XML files when they have a matching node. From what I gather this could be accomplished with many different languages… is there a PHP or an AJAX way to do this? From other posts on SO I see XSLT solutions.. that I dont really get. Is this the best/preferred method? If so, know of any helpful XSLT tutorials?

For example XML-1 is like :

<FOO>
    </A>
    </B>
    </C>
    </D>
</FOO>

and XML-2 :

<FOO>    
    </B>
    </E>
</FOO>

What would be the best approach for checking where <B>==<B> then add <E>

Update

Well I cant get this to work with my hypothetical example and thought I would update with what I am really doing to see if anyone can help me figure this. I have tried the methods from below and others I have found on SO with no luck.

The real schema is like :

file1.xml

<?xml version="1.0"?>
<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
 ...
</DATA>

file2.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl" ?>
<DATA>
  <ITEM>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
  </ITEM>
  ....
</DATA>

What I need to figure out is to have a new XML file generated that would merge these nodes with identical SYTLE_COLOR and look like:

<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
    <STATUS>Disabled</STATUS>
  </ITEM>

I tried creating a merge.xsl that looks like :

<?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:output indent="yes"/>
  <xsl:variable name="with" select="'file-2.xml'" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="scene">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="info" select="document($with)/DATA/ITEM[STYLE_COLOR=current()/STYLE_COLOR]/." />
      <xsl:for-each select="$info/*">
        <xsl:if test="name()!='STYLE_COLOR'">
          <xsl:copy-of select="." />
        </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

I also tried a merge like this:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:variable name="input2" select="document('file-2.xml')/DATA/ITEM"/>
    <xsl:template match="STYLE_COLOR">
        <xsl:copy>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="$input2/*[name()=name(current())]">
                <xsl:copy-of select="$input2/*"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet> 

Neither of these methods are working.. sorry XSLT is very new to me so I am not sure what I am doing and would really appreciate some hand holding on this one.

  • 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-23T04:31:08+00:00Added an answer on May 23, 2026 at 4:31 am

    This is the original transform slightly modified to adapt the new requirements. The merge is performed by checking against file2.xml elements. For the current ITEM in file1, a children ITEM in file2 will be merged only if not present in the file1.


    [XSLT 1.0]

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:variable name="input2" select="document('test_input2.xml')/DATA"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="ITEM">
            <xsl:variable name="item" select="
                $input2/ITEM[STYLE_COLOR=current()/STYLE_COLOR]"/>
            <xsl:variable name="ITEM" select="."/>
    
            <xsl:if test="$item">
                <xsl:copy>
    
                    <xsl:for-each select="$item/*">
                        <xsl:if test="count($ITEM/*[name()=name(current())])=0">
                            <xsl:copy-of select="." />
                        </xsl:if>
                    </xsl:for-each>
    
                    <xsl:apply-templates select="*"/>
                </xsl:copy>
            </xsl:if>
        </xsl:template>
    
    </xsl:stylesheet> 
    

    Applied on this input1.xml:

    <DATA>
      <ITEM>
        <PRODUCT_TYPE>simple</PRODUCT_TYPE>
        <STYLE_COLOR>1524740007</STYLE_COLOR>
        <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
        <CLASS_NAME>FOOTWEAR</CLASS_NAME>
        <STATUS>Disabled</STATUS>
      </ITEM>
      <ITEM>
        <PRODUCT_TYPE>simple</PRODUCT_TYPE>
        <STYLE_COLOR>1524740008</STYLE_COLOR>
        <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
        <CLASS_NAME>FOOTWEAR</CLASS_NAME>
        <STATUS>Disabled</STATUS>
      </ITEM>
      <ITEM>
        <PRODUCT_TYPE>simple</PRODUCT_TYPE>
        <STYLE_COLOR>777</STYLE_COLOR>
        <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
        <CLASS_NAME>FOOTWEAR</CLASS_NAME>
        <STATUS>Disabled</STATUS>
      </ITEM>
    </DATA>
    

    and input2.xml to merge, produces:

    <DATA>
      <ITEM>
        <STYLE_COLOR>1524740007</STYLE_COLOR>
        <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
        <CLASS_NAME>XXX</CLASS_NAME>
        <OTHER>YYY</OTHER>
      </ITEM>
      <ITEM>
        <STYLE_COLOR>1524740008</STYLE_COLOR>
        <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
      </ITEM>
    </DATA>
    

    produces:

    <DATA>
       <ITEM>
          <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
          <OTHER>YYY</OTHER>
          <PRODUCT_TYPE>simple</PRODUCT_TYPE>
          <STYLE_COLOR>1524740007</STYLE_COLOR>
          <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
          <CLASS_NAME>FOOTWEAR</CLASS_NAME>
          <STATUS>Disabled</STATUS>
       </ITEM>
       <ITEM>
          <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
          <PRODUCT_TYPE>simple</PRODUCT_TYPE>
          <STYLE_COLOR>1524740008</STYLE_COLOR>
          <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
          <CLASS_NAME>FOOTWEAR</CLASS_NAME>
          <STATUS>Disabled</STATUS>
       </ITEM>
    </DATA>
    

    Notice that:

    • the transform does not override existing elements for a given ITEM, just copy the missing ones
    • ITEM in input1.xml is copied in output only if has a match in input2.xml
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find a way to hide HTML Rows (or Tables) from view
I need a Java way to find a running Win process from which I
I have two documents both are similar but I need to find an elegant
I need to find a way to crawl one of our company's web applications
I need to find a way to get at the request/response streams inside of
I need to find a way to monitor the status of a list of
I need to find a fairly efficient way to detect syllables in a word.
I need a quick way to find out if a given port is open
I'm trying to connect the values of two join tables that I have and
I need to find a way to accurately calculate the byte size of the

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.