I've worked out how to merge two XML file and amend the matching attribute.
I'm now struggling to work out how to add file2 node (based on attribute name) if it does not exist in file1
!--File1 xml -->
<stylesheet>
<variable name="Test1" />
<variable name="Test2" select="'yy'"/>
<variable name="Test3" select="'xx'"/>
</sytlesheet>
<!--File2 xml -->
<stylesheet>
<variable name="Test" select="'x'" />
<variable name="Test2" select="'y'" />
<variable name="Test3" select="'z'" />
<variable name="Test4" select="'dd'" />
</sytlesheet>
<!--Expected xml result-->
<stylesheet>
<variable name="Test1" />
<variable name="Test" select="'x'" />
<variable name="Test2" select="'y'" />
<variable name="Test3" select="'z'" />
<variable name="Test4" select="'dd'" />
</sytlesheet>
Here's the xsl file I have:
<xsl:param name="fileName" select="'file2'" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="stylesheet/variable">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:if test="document($fileName)/stylesheet/variable[@name = current()/@name]">
<xsl:attribute name="value">
<xsl:value-of select="document($fileName)/stylesheet/variable[@name = current()/@name]/@select"/>
</xsl:attribute>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I found how to Merge two xml files with XSLT but unable to work out how to apply propose solution to my xsl. Anyone can help?