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?
(See my comment on the “one-way-merge” on the OP.) Here’s my (very inefficient) solution to the merge problem:
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:
In file1.xml, I have this:
In file2.xml, I have this:
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):
Running this gives me this output: