XSLT - Copy all nodes and add extra node in copied nodes -
i looking xslt(1.0) code below input , output xml.
in output xml, there can child node under c6 element. in below xml, have put cn element name.
input xml -
<?xml version = "1.0" encoding = "utf-8"?> <root> <input> <c2> <c3> <c4>c4</c4> </c3> </c2> </input> <output> <c5> <c6> <cn> <t1></t1> <t2></t2> </cn> </c6> <c6> <cn> <t1></t1> <t2></t2> </cn> </c6> </c5> </output> </root>
desired output xml-
<root> <output> <c5> <c6> <!-- have child node. putting example cn child node name.--> <cn> <t1></t1> <t2></t2> <c3> <c4>c4</c4> <newnode>current number of cn node 1</newnode> <newnode1>total number of c6 nodes 2.</newnode1> </c3> </cn> </c6> <c6> <cn> <t1></t1> <t2></t2> <c3> <c4>c4</c4> <newnode>current number of cn node 2</newnode> <newnode1>total number of c6 nodes 2.</newnode1> </c3> </cn> </c6> </c5> </output> </root>
thank in advance.
use following stylesheet:
<?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" indent="yes" /> <xsl:template match="c6/*"> <xsl:copy> <xsl:variable name="v1" select="count(../preceding-sibling::*)+1"/> <xsl:variable name="v2" select="count(../../*)"/> <xsl:apply-templates/> <xsl:apply-templates select="../../../../input/c2/c3"> <xsl:with-param name="v1" select="$v1"/> <xsl:with-param name="v2" select="$v2"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="c3"> <xsl:param name="v1"/> <xsl:param name="v2"/> <xsl:copy> <xsl:apply-templates/> <newnode><xsl:value-of select="$v1"/></newnode> <newnode1><xsl:value-of select="$v2"/></newnode1> </xsl:copy> </xsl:template> <xsl:template match="input"/> <xsl:template match="@*|node()"> <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment