Reputation: 221
I'm trying to created an xsl that will copy file1, search file2 for matching file1 node and change that node's attribute to file2 value. I'm struggle to get the below code to work. It transform the first node correctly but on the second node it uses the previously found attribute.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<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:for-each select=".">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:if test="document($fileName)/stylesheet/variable[@name = @name]">
<xsl:attribute name="value">
<xsl:value-of select="document($fileName)/stylesheet/variable[@name = @name]/@select"/>
</xsl:attribute>
</xsl:if>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Here's the xml file I'm trying to merge into one file
<!--File1 xml -->
<stylesheet>
<variable name="Test" />
<variable name="Test2" select="'yy'"/>
<variable name="Test3" select="'xx'"/>
</sytlesheet>
<!--File2 xml -->
<stylesheet>
<variable name="Test" select="'x'" />
<variable name="Test2" select="'y'" />
</sytlesheet>
Any idea where I'm going wrong?
Upvotes: 0
Views: 38
Reputation: 167726
variable[@name = @name]
is not what you want, you want variable[@name = current()/@name]
. The use of <xsl:for-each select=".">
is superfluous.
Upvotes: 2