<person>
<xsl:apply-templates select="person/name"/> <!-- first.xml is the main input -->
<xsl:apply-templates select="document('second.xml')/person/name"/>
</person>
Subject:Merge two xml files Author:V G Date:12 Oct 2004 10:39 AM
Hi
I did the same at first but this code just copy first.xml and then the second.xml
My logic is that I want to overwrite the elements from first.xml if they appear in the second.xml
e.g.
in the first.xml the middle name of the John is Smith but in the secon.xml it is Bush
in the result (as I described) John must be with Bush as its middle name
<!-- check if "name/first" is present in the second.xml -->
<xsl:variable name="name_from_second" select="key('name_by_first', $first)"/>
<xsl:choose>
<xsl:when test="count($name_from_second) > 0"> <!-- it is present then use the second definiton -->
<xsl:apply-templates select="$name_from_second"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
<!-- loop on second.xml to see if we have additional "name" element to add -->
<xsl:for-each select="document('second.xml')/person/name">
<xsl:variable name="first" select="first"/>
<xsl:variable name="name" select="."/>
<!-- check if "name/first" is present in the first.xml -->
<xsl:for-each select="document('first.xml')">
<xsl:if test="count(key('name_by_first', $first))=0"> <!-- first is not present then add it -->
<xsl:apply-templates select="$name"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</person>
</xsl:template>