Find parent attribute and append to child



I need to transform some xml files and one of the task is to find an attribute in a specified node and append the attribute value for the child node. For example here is the original xml:



<ParentNode attribute="someValue">
<ChildNode other_att="foo">
<OtherNode />
</ChildNode>
</ParentNode>


And that's the desired output:



<ParentNode attribute="someValue">
<ChildNode other_att="foo" appended_attribute="someValue">
<OtherNode />
</ChildNode>
</ParentNode>


I'm just getting familiar with xslt. Here is what I tried to do:



<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="ChildNode">
<xsl:copy>
<xsl:attribute name="appended_attribute">
<xsl:value-of select="'someValue'"/>
</xsl:attribute>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


But it just selected the childnode and append the attribute with hardwired value.


Any advice approciated. Thank you!


No comments:

Post a Comment