I have two xml files.
File 1 -
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<School>
<section id="12" name="Apple"/>
<section id="50" name="Newton"/>
</School>
<Students>
<roll no="111" name="Smith"/>
<roll no="122" name="Alan"/>
<roll no="20" name="Bruce"/>
</Students>
<Teachers>
<Math>
<emp id="55" name="Karen"/>
<emp id="2" name="David"/>
</Math>
<Science>
<emp id="1" name="Thomas"/>
</Science>
</Teachers>
<Sports>
<Indoor>
<Boardgame>
<game id="12" name="Chess"/>
</Boardgame>
<Arcade>
<game id="3" name="Car Racing"/>
</Arcade>
</Indoor>
<Outdoor>
<Field>
<game id="1" name="Football"/>
<game id="100" name="Cricket"/>
</Field>
<Court>
<game id="2" name="Tennis"/>
</Court>
</Outdoor>
</Sports>
</Root>
File 2 -
<?xml version="1.0" encoding="UTF-8"?>
<Updates>
<School>
<section id="12" name="Orange"/>
</School>
<Students>
<roll no="122" name="Sam"/>
</Students>
<Teachers>
<Math>
<emp id="300" name="Steve" />
</Math>
</Teachers>
<Sports>
<Indoor>
<Boardgame>
<game id="37" name="Monopoly"/>
</Boardgame>
<Boardgame2>
<game id="36" name="Ludo"/>
</Boardgame2>
</Indoor>
<Outdoor>
<Field>
<game id="1" name="Football"/>
<game id="100" name="Bull Fighting"/>
</Field>
<Court>
<game id="19" name="Badminton"/>
</Court>
</Outdoor>
<Computer>
<game id="10" name="AOE" />
</Computer>
</Sports>
</Updates>
I need to merge the files so that I get the following output. Entries in file2 would overwrite those in file1 if id/no are matching.New elements would be added as required from file2 in the output under the proper hierarchy.
Output of Transformation -
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<School>
<section id="12" name="Orange"/>
<section id="50" name="Newton"/>
</School>
<Students>
<roll no="111" name="Smith"/>
<roll no="122" name="Sam"/>
<roll no="20" name="Bruce"/>
</Students>
<Teachers>
<Math>
<emp id="55" name="Karen"/>
<emp id="2" name="David"/>
<emp id="300" name="Steve" />
</Math>
<Science>
<emp id="1" name="Thomas"/>
</Science>
</Teachers>
<Sports>
<Indoor>
<Boardgame>
<game id="12" name="Chess"/>
<game id="37" name="Monopoly"/>
</Boardgame>
<Arcade>
<game id="3" name="Car Racing"/>
</Arcade>
<Boardgame2>
<game id="36" name="Ludo"/>
</Boardgame2>
</Indoor>
<Outdoor>
<Field>
<game id="1" name="Football"/>
<game id="100" name="Bull Fighting"/>
</Field>
<Court>
<game id="2" name="Tennis"/>
<game id="19" name="Badminton"/>
</Court>
</Outdoor>
<Computer>
<game id="10" name="AOE" />
</Computer>
</Sports>
</Root>
Below is the XSLT, but it works only for updates, not for inserts.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/">
<xsl:apply-templates select="node()">
<xsl:with-param name="doc-context" select="document('file2.xml')/node()" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="node()">
<xsl:param name="doc-context" />
<xsl:variable name="id" select="@id" />
<xsl:variable name="no" select="@no" />
<xsl:copy>
<xsl:copy-of select="@*|$doc-context[@id = $id or @no = $no]/@*" />
<xsl:apply-templates select="node()">
<xsl:with-param name="doc-context" select="$doc-context/node()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>