I am completely new to XSLT so please bear with me.
I have two xml files that I am attempting to concatenate together using XSLT. I would like to combine the files such that any values specified in the second file override the first. E.g.
firstFile.xml
<person>
<person-name>Sandy</person-name>
<person-age>21</person-age>
</person>
<person>
<person-name>Bob</person-name>
<person-age>15</person-age>
</person>
override.xml
<person>
<person-name>Bob</person-name>
<person-age>21</person-age>
</person>
Result:
<person>
<person-name>Sandy</person-name>
<person-age>21</person-age>
</person>
<person>
<person-name>Bob</person-name>
<person-age>21</person-age>
</person>
My template for concatenating the 2 files is as follows:
<xsl:template match="/">
<!-- MainFile -->
<xsl:copy-of select="/*"/>
<!-- Overrides-->
<xsl:copy-of select="document($overrideFile)/*"/>
</xsl:template>
I was attempting to setup a for-each loop such that before copying each person in firstFile.xml check if there is a corresponding node in override.xml, but was unsuccessful.
Any tips would be greatly appreciated
This transformation:
when applied on the first of the provided XML documents (wrapped into a single top element — to be made a well-formed XML document):
and being passed as parameter the filename where the second document (again wrapped into a top element) resides — here is the corrected second document:
c:/temp/delete/override.xml:
produces the wanted, correct result:
II. A shorter, but less flexible solution — no identity rule and no
xsl:apply-templates: