XSL copy template and add new attribute



The input XML:



<a>
<b1>
<c1 width="500" height="200" />
</b1>
<b2 />
</a>


I want to copy all of the attributes from b1/c1 to b2/c1 AND add a new attribute (length). The output XML should be:



<a>
<b1>
<c1 width="500" height="200" />
</b1>
<b2>
<c1 width="500" height="200" length="2" />
</b2>
</a>


I have a code, which copy all from b1/c1 to b2/c2 BUT without adding a new atttribute (length):



<xsl:template match="/a/b2">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:copy-of select="/a/b1/c1" />
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>


And I tried to add attribute to the copy part, but it doesn't work:



<xsl:template match="/a/b2">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:copy-of select="/a/b1/c1" >
<xsl:attribute name="length">2</xsl:attribute>
</xsl:copy-of>
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>

No comments:

Post a Comment