xml - XSLT copy - make 2 nodes from 1 node -
i have following xml:
<records> <record> <id>111</id> <amount>123.45</amount> <taxcode>a</taxcode> </record> </records>
i need copy nodes in above create additional copy of of nodes have value of, lets say, taxcode. how achive following:
<records> <record> <id>111</id> <amount>123.45</amount> <taxcode>a</taxcode> </record> <!-- copy of node above amount , taxcode changed --> <record> <id>111</id> <amount>-123.45</amount> <taxcode>b</taxcode> </record> </records>
i tried using following simple xsl copies once:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="no" indent="yes" /> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="records/taxcode[text() = 'a']" > <xsl:copy select="node()" /> </xsl:template> </xsl:stylesheet>
any appreciated.
how about:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="records"> <xsl:copy> <xsl:apply-templates/> <xsl:apply-templates select="record[taxcode='a']" mode="b"/> </xsl:copy> </xsl:template> <xsl:template match="record" mode="b"> <xsl:copy> <xsl:copy-of select="id"/> <amount> <xsl:value-of select="-amount"/> </amount> <taxcode>b</taxcode> </xsl:copy> </xsl:template> </xsl:stylesheet>
or, if prefer:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="record[taxcode='a']" > <xsl:copy> <xsl:apply-templates/> </xsl:copy> <xsl:copy> <xsl:copy-of select="id"/> <amount> <xsl:value-of select="-amount"/> </amount> <taxcode>b</taxcode> </xsl:copy> </xsl:template> </xsl:stylesheet>