title: Merge XML files with same structure and different data
body: <p>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:</p>
  
  <pre><code>file1.xml
  &lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;BROADRIDGE&gt;
      &lt;SECURITY CUSIP='CUSIP1' DESCRIPT='CUSIP1'&gt;
          &lt;CUSTOMER ID='M1'/&gt;
          &lt;CUSTOMER ID='M2'/&gt;
          &lt;CUSTOMER ID='M3'/&gt;
      &lt;/SECURITY&gt;
      &lt;SECURITY CUSIP='CUSIP3' DESCRIPT='CUSIP3'&gt;
          &lt;CUSTOMER ID='M4'/&gt;
          &lt;CUSTOMER ID='M5'/&gt;
          &lt;CUSTOMER ID='M6'/&gt;
      &lt;/SECURITY&gt;
  &lt;/BROADRIDGE&gt;
  
  file2.xml
  &lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;BROADRIDGE&gt;
      &lt;SECURITY CUSIP='CUSIP1' DESCRIPT='CUSIP1'&gt;
          &lt;CUSTOMER ID='B1'/&gt;
          &lt;CUSTOMER ID='B2'/&gt;
          &lt;CUSTOMER ID='B3'/&gt;
      &lt;/SECURITY&gt;
      &lt;SECURITY CUSIP='CUSIP2' DESCRIPT='CUSIP2'&gt;
          &lt;CUSTOMER ID='B4'/&gt;
          &lt;CUSTOMER ID='B5'/&gt;
          &lt;CUSTOMER ID='B6'/&gt;
      &lt;/SECURITY&gt;
  &lt;/BROADRIDGE&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;BROADRIDGE&gt;
      &lt;SECURITY CUSIP="CUSIP1"&gt;
          &lt;CUSTOMER ID="M1"/&gt;
          &lt;CUSTOMER ID="M2"/&gt;
          &lt;CUSTOMER ID="M3"/&gt;
          &lt;CUSTOMER ID='B1'/&gt;
          &lt;CUSTOMER ID='B2'/&gt;
          &lt;CUSTOMER ID='B3'/&gt;
      &lt;/SECURITY&gt;
      &lt;SECURITY CUSIP="CUSIP3"&gt;
          &lt;CUSTOMER ID="M4"/&gt;
          &lt;CUSTOMER ID="M5"/&gt;
          &lt;CUSTOMER ID="M6"/&gt;
      &lt;/SECURITY&gt;
      &lt;SECURITY CUSIP="CUSIP2"&gt;
          &lt;CUSTOMER ID="B4"/&gt;
          &lt;CUSTOMER ID="B5"/&gt;
          &lt;CUSTOMER ID="B6"/&gt;
      &lt;/SECURITY&gt;
  &lt;/BROADRIDGE&gt;
  </code></pre>
  
  <p>I've defined the folling xml to joing them:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;                                  
  &lt;MASTERFILE&gt;
     &lt;FILE&gt;\file1.xml&lt;/FILE&gt;
     &lt;FILE&gt;\file2.xml&lt;/FILE&gt;
  &lt;/MASTERFILE&gt;
  </code></pre>
  
  <p>And the following XSL to do the merge:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:template match="/MASTERFILE"&gt;
          &lt;BROADRIDGE&gt;
              &lt;xsl:variable name="securities" select="document(FILE)/BROADRIDGE/SECURITY"/&gt;
              &lt;xsl:for-each select="$securities"&gt;
                  &lt;xsl:if test="generate-id(.) = generate-id($securities[@CUSIP=current()/@CUSIP])"&gt;
                      &lt;SECURITY&gt;
                          &lt;xsl:attribute name="CUSIP" &gt;&lt;xsl:value-of select="@CUSIP"/&gt;&lt;/xsl:attribute&gt;
                          &lt;xsl:for-each select="CUSTOMER"&gt;
                              &lt;CUSTOMER&gt;
                                  &lt;xsl:attribute name="ID" &gt;&lt;xsl:value-of select="@ID"/&gt;&lt;/xsl:attribute&gt;
                              &lt;/CUSTOMER&gt;
                          &lt;/xsl:for-each&gt;
                      &lt;/SECURITY&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
          &lt;/BROADRIDGE&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>But I'm getting the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;BROADRIDGE&gt;
      &lt;SECURITY CUSIP="CUSIP1"&gt;
          &lt;CUSTOMER ID="M1"/&gt;
          &lt;CUSTOMER ID="M2"/&gt;
          &lt;CUSTOMER ID="M3"/&gt;
      &lt;/SECURITY&gt;
      &lt;SECURITY CUSIP="CUSIP3"&gt;
          &lt;CUSTOMER ID="M4"/&gt;
          &lt;CUSTOMER ID="M5"/&gt;
          &lt;CUSTOMER ID="M6"/&gt;
      &lt;/SECURITY&gt;
      &lt;SECURITY CUSIP="CUSIP2"&gt;
          &lt;CUSTOMER ID="B4"/&gt;
          &lt;CUSTOMER ID="B5"/&gt;
          &lt;CUSTOMER ID="B6"/&gt;
      &lt;/SECURITY&gt;
  &lt;/BROADRIDGE&gt;
  </code></pre>
  
  <p>Any idea why it's not merging the CUSTOMERS from both file for SECURITY with CUSIP = 
  CUSIP1?</p>