(См. Мой комментарий по поводу "одностороннего слияния" на OP.) Вот мое (очень неэффективное) решение проблемы слияния:
<?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() < $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>
Обратите внимание, что таблица стилей не предполагает какого-либо конкретного документа - она просто загружает два файла как переменные. Можно улучшить дизайн xslt, задав параметры для загружаемых XML-документов
Чтобы применить объединение к нескольким документам, вы можете создать файл, скажем, master.xml, в котором перечислены все файлы для обработки следующим образом:
<?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>
В 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>
merge.xslt является модифицированной версией более ранней версии, которая теперь может обрабатывать различное количество файлов (файлов, перечисленных в 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 < $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() < $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>
Запуск этого дает мне этот вывод:
<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>