I'm looking to do some really simply XSLT on my XML document, which has a structure like:
<XML>
<TAG1 attribute1="A">
<IRRELEVANT />
<TAG2 attribute2="B" attribute3="34" />
</TAG1>
</XML>
And am trying to make a XSLT which does the following:
- Copy everything
- If TAG2@attribute1="A" AND TAG2@attribute2="B" AND there is no TAG2@attribute4, then add @attribute4 to TAG2 and have its value to be the value of TAG2@attribute3
My unsuccessful attempt is below. The error I'm getting is "xsl:attribute: Cannot add attributes to an element if children have been already added to the element."
Thanks!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="newDocumentHeader" />
<!-- copy idiom copying ALL nodes -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<!-- override for special case -->
<xsl:template match="TAG1[@attribute1='A']/TAG2[@attribute2='B'][not(@attribute4)]">
<xsl:attribute name="attribute4">
<xsl:value-of select="@attribute3" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>