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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:45:00+00:00 2026-05-13T09:45:00+00:00

I’m trying to merge two file that have the same structure, and some data

  • 0

I’m trying to merge two file that have the same structure, and some data in common. So if a node has the same name in both files, a new node should be created with the children of both original nodes. The original files are the following:

file1.xml
<?xml version='1.0' encoding='UTF-8'?>
<BROADRIDGE>
    <SECURITY CUSIP='CUSIP1' DESCRIPT='CUSIP1'>
        <CUSTOMER ID='M1'/>
        <CUSTOMER ID='M2'/>
        <CUSTOMER ID='M3'/>
    </SECURITY>
    <SECURITY CUSIP='CUSIP3' DESCRIPT='CUSIP3'>
        <CUSTOMER ID='M4'/>
        <CUSTOMER ID='M5'/>
        <CUSTOMER ID='M6'/>
    </SECURITY>
</BROADRIDGE>

file2.xml
<?xml version='1.0' encoding='UTF-8'?>
<BROADRIDGE>
    <SECURITY CUSIP='CUSIP1' DESCRIPT='CUSIP1'>
        <CUSTOMER ID='B1'/>
        <CUSTOMER ID='B2'/>
        <CUSTOMER ID='B3'/>
    </SECURITY>
    <SECURITY CUSIP='CUSIP2' DESCRIPT='CUSIP2'>
        <CUSTOMER ID='B4'/>
        <CUSTOMER ID='B5'/>
        <CUSTOMER ID='B6'/>
    </SECURITY>
</BROADRIDGE>

The idea is to create a new XML file with the same structure that contains the information from both files, merging those SECURITY nodes that have the same CUSIP attribute. In this case the result should be the following:

<?xml version="1.0" encoding="UTF-8"?>
<BROADRIDGE>
    <SECURITY CUSIP="CUSIP1">
        <CUSTOMER ID="M1"/>
        <CUSTOMER ID="M2"/>
        <CUSTOMER ID="M3"/>
        <CUSTOMER ID='B1'/>
        <CUSTOMER ID='B2'/>
        <CUSTOMER ID='B3'/>
    </SECURITY>
    <SECURITY CUSIP="CUSIP3">
        <CUSTOMER ID="M4"/>
        <CUSTOMER ID="M5"/>
        <CUSTOMER ID="M6"/>
    </SECURITY>
    <SECURITY CUSIP="CUSIP2">
        <CUSTOMER ID="B4"/>
        <CUSTOMER ID="B5"/>
        <CUSTOMER ID="B6"/>
    </SECURITY>
</BROADRIDGE>

I’ve defined the folling xml to joing them:

<?xml version="1.0"?>                                  
<MASTERFILE>
   <FILE>\file1.xml</FILE>
   <FILE>\file2.xml</FILE>
</MASTERFILE>

And the following XSL to do the merge:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/MASTERFILE">
        <BROADRIDGE>
            <xsl:variable name="securities" select="document(FILE)/BROADRIDGE/SECURITY"/>
            <xsl:for-each select="$securities">
                <xsl:if test="generate-id(.) = generate-id($securities[@CUSIP=current()/@CUSIP])">
                    <SECURITY>
                        <xsl:attribute name="CUSIP" ><xsl:value-of select="@CUSIP"/></xsl:attribute>
                        <xsl:for-each select="CUSTOMER">
                            <CUSTOMER>
                                <xsl:attribute name="ID" ><xsl:value-of select="@ID"/></xsl:attribute>
                            </CUSTOMER>
                        </xsl:for-each>
                    </SECURITY>
                </xsl:if>
            </xsl:for-each>
        </BROADRIDGE>
    </xsl:template>
</xsl:stylesheet>

But I’m getting the following:

<?xml version="1.0" encoding="UTF-8"?>
<BROADRIDGE>
    <SECURITY CUSIP="CUSIP1">
        <CUSTOMER ID="M1"/>
        <CUSTOMER ID="M2"/>
        <CUSTOMER ID="M3"/>
    </SECURITY>
    <SECURITY CUSIP="CUSIP3">
        <CUSTOMER ID="M4"/>
        <CUSTOMER ID="M5"/>
        <CUSTOMER ID="M6"/>
    </SECURITY>
    <SECURITY CUSIP="CUSIP2">
        <CUSTOMER ID="B4"/>
        <CUSTOMER ID="B5"/>
        <CUSTOMER ID="B6"/>
    </SECURITY>
</BROADRIDGE>

Any idea why it’s not merging the CUSTOMERS from both file for SECURITY with CUSIP =
CUSIP1?

  • 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-13T09:45:01+00:00Added an answer on May 13, 2026 at 9:45 am

    (See my comment on the “one-way-merge” on the OP.) Here’s my (very inefficient) solution to the merge problem:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="set1" select="document('file1.xml')/BROADRIDGE/SECURITY"/>
        <xsl:variable name="set2" select="document('file2.xml')/BROADRIDGE/SECURITY"/>
    
        <xsl:template match="/">
            <BROADRIDGE>
                <!-- walk over all relevant nodes -->
                <xsl:for-each select="$set1 | $set2">
                    <xsl:variable name="position" select="position()"/>
                    <xsl:variable name="cusip" select="@CUSIP"/>
                    <!-- if we see this CUSIP for the first time, --> 
                    <xsl:if test="count($nodes[position() &lt; $position][@CUSIP = $cusip])=0">
                        <SECURITY>                            
                            <xsl:attribute name="CUSIP"><xsl:value-of select="$cusip"/></xsl:attribute>
                            <!-- copy nodes from both sets with matching attribute -->
                            <xsl:copy-of select="$set1[@CUSIP = $cusip]/*"/>
                            <xsl:copy-of select="$set2[@CUSIP = $cusip]/*"/>
                        </SECURITY>
                    </xsl:if>
                </xsl:for-each>
            </BROADRIDGE>
        </xsl:template>
    </xsl:stylesheet>
    

    Note that the stylesheet does not suppose any particular document – it simply loads the two files as variables. One can improve th xslt design by parameterizing the urls for the to be loaded XML documents

    To apply the merge to multiple documents, you can create a file, say master.xml that lists all the files to process like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="merge.xslt"?>
    <files>
      <file>file1.xml</file>
      <file>file2.xml</file>
      ...
      <file>fileN.xml</file>    
    </files>
    

    In file1.xml, I have this:

    <?xml version='1.0' encoding='UTF-8'?>
    <BROADRIDGE>
      <SECURITY CUSIP='CUSIP1' DESCRIPT='CUSIP1'>
        <CUSTOMER ID='M1'/>
        <CUSTOMER ID='M2'/>
        <CUSTOMER ID='M3'/>
      </SECURITY>
      <SECURITY CUSIP='CUSIP3' DESCRIPT='CUSIP3'>
        <CUSTOMER ID='M4'/>
        <CUSTOMER ID='M5'/>
        <CUSTOMER ID='M6'/>
      </SECURITY>
    </BROADRIDGE>
    

    In file2.xml, I have this:

    <?xml version='1.0' encoding='UTF-8'?>
    <BROADRIDGE>
      <SECURITY CUSIP='CUSIP1' DESCRIPT='CUSIP1'>
        <CUSTOMER ID='B1'/>
        <CUSTOMER ID='B2'/>
        <CUSTOMER ID='B3'/>
      </SECURITY>
      <SECURITY CUSIP='CUSIP2' DESCRIPT='CUSIP2'>
        <CUSTOMER ID='B4'/>
        <CUSTOMER ID='B5'/>
        <CUSTOMER ID='B6'/>
      </SECURITY>
    </BROADRIDGE>
    

    the merge.xslt is a modified version of the earlier one, which is now capable of processing a variable number of files (the files listed in master.xml):

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
      <xsl:call-template name="merge-files"/>
    </xsl:template>
    
    <!-- loop through file names, load documents -->
    <xsl:template name="merge-files">
      <xsl:param name="files" select="/files/file/text()"/>
      <xsl:param name="num-files" select="count($files)"/>
      <xsl:param name="curr-file" select="0"/>
      <xsl:param name="set" select="/*[0]"/>
      <xsl:choose> <!-- if we still have files, concat them to $set -->
        <xsl:when test="$curr-file &lt; $num-files">
          <xsl:call-template name="merge-files">
            <xsl:with-param name="files" select="$files"/>
            <xsl:with-param name="num-files" select="$num-files"/>
            <xsl:with-param name="curr-file" select="$curr-file + 1"/>
            <xsl:with-param name="set" select="$set | document($files[$curr-file+1])/BROADRIDGE/SECURITY"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise> <!-- no more files, start merging. -->
          <xsl:call-template name="merge">
            <xsl:with-param name="nodes" select="$set"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    <!-- perform the actual merge -->
    <xsl:template name="merge">
      <xsl:param name="nodes"/>
      <BROADRIDGE>
        <xsl:for-each select="$nodes"> <!-- look at all possible nodes to merge -->
          <xsl:variable name="position" select="position()"/>
          <xsl:variable name="cusip" select="@CUSIP"/>
    
          <!-- when we encounter this id for the 1st time -->
          <xsl:if test="count($nodes[position() &lt; $position][@CUSIP = $cusip])=0"> 
            <SECURITY>
              <xsl:attribute name="CUSIP"><xsl:value-of select="$cusip"/></xsl:attribute>
              <!-- copy all node data related to this cusip here -->
              <xsl:for-each select="$nodes[@CUSIP = $cusip]">
                <xsl:copy-of select="*"/>
              </xsl:for-each>
            </SECURITY>
          </xsl:if>
        </xsl:for-each>
      </BROADRIDGE>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Running this gives me this output:

    <BROADRIDGE>
      <SECURITY CUSIP="CUSIP1">
        <CUSTOMER ID="M1"/>
        <CUSTOMER ID="M2"/>
        <CUSTOMER ID="M3"/>
        <CUSTOMER ID="B1"/>
        <CUSTOMER ID="B2"/>
        <CUSTOMER ID="B3"/>
      </SECURITY>
      <SECURITY CUSIP="CUSIP3">
        <CUSTOMER ID="M4"/>
        <CUSTOMER ID="M5"/>
        <CUSTOMER ID="M6"/>
      </SECURITY>
      <SECURITY CUSIP="CUSIP2">
        <CUSTOMER ID="B4"/>
        <CUSTOMER ID="B5"/>
        <CUSTOMER ID="B6"/>
      </SECURITY>
    </BROADRIDGE>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're not changing ptArray[0] itself, because that's a reference to… May 14, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer If doing as Greg suggests and putting it in a… May 14, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer UPDATED 2 Tested on Internet Explorer 6 and later Chrome… May 14, 2026 at 4:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.