XSL - Altering child attribute with original parent attribute
XML
<?xml version="1.0" encoding="utf-8"?> <Items xmlns="http://www.abc.com"
xmlns:myns="http://www.abc.com"> <Item attr="1"> <field1>My Content 1</field1>
</Item> <Item attr="2"> <field1>My Content 1</field1> </Item> <Item attr="3">
<field1>My Content 1</field1> <field2 innerAttr="a">My Content 2</field2>
</Item> </Items>
XSL
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="http://www.abc.com"
xmlns="http://www.abc.com" > <xsl:output omit-xml-declaration="no"
indent="yes"/> <xsl:namespace-alias stylesheet-prefix="#default"
result-prefix="#default"/> <xsl:template match="@* | node()"> <xsl:copy>
<xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
<xsl:template match="/myns:Items/myns:Item"> <xsl:variable name="OriginalAttr"
select="@attr"/> <xsl:choose> <xsl:when test="./myns:field2/@innerAttr">
<xsl:copy> <xsl:apply-templates select="@*"/> <xsl:for-each select="*"> <xsl:if
test="local-name()='field1'"> <xsl:copy> <xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/> </xsl:copy> </xsl:if> <xsl:if
test="local-name()='field2'"> <xsl:copy> <xsl:attribute name="innerAttr"> <!--
copy from parent attribute to child attribute--> <xsl:value-of
select="$OriginalAttr"/> </xsl:attribute> <xsl:apply-templates select="
node()"/> </xsl:copy> </xsl:if> </xsl:for-each> </xsl:copy> </xsl:when>
<xsl:otherwise> <xsl:copy> <xsl:apply-templates select="@* | node()"/>
</xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?> <Items xmlns="http://www.abc.com"
xmlns:myns="http://www.abc.com"> <Item attr="1"> <field1>My Content 1</field1>
</Item> <Item attr="2"> <field1>My Content 1</field1> </Item> <Item attr="3">
<field1>My Content 1</field1> <field2 innerAttr="a">My Content 2</field2>
</Item> </Items>
XSL
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="http://www.abc.com"
xmlns="http://www.abc.com" > <xsl:output omit-xml-declaration="no"
indent="yes"/> <xsl:namespace-alias stylesheet-prefix="#default"
result-prefix="#default"/> <xsl:template match="@* | node()"> <xsl:copy>
<xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
<xsl:template match="/myns:Items/myns:Item"> <xsl:variable name="OriginalAttr"
select="@attr"/> <xsl:choose> <xsl:when test="./myns:field2/@innerAttr">
<xsl:copy> <xsl:apply-templates select="@*"/> <xsl:for-each select="*"> <xsl:if
test="local-name()='field1'"> <xsl:copy> <xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/> </xsl:copy> </xsl:if> <xsl:if
test="local-name()='field2'"> <xsl:copy> <xsl:attribute name="innerAttr"> <!--
copy from parent attribute to child attribute--> <xsl:value-of
select="$OriginalAttr"/> </xsl:attribute> <xsl:apply-templates select="
node()"/> </xsl:copy> </xsl:if> </xsl:for-each> </xsl:copy> </xsl:when>
<xsl:otherwise> <xsl:copy> <xsl:apply-templates select="@* | node()"/>
</xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Comments