Prevent multiple definitions of element in XSLT -


i attempting write xslt stylesheet copies 1 xml document using number of generic templates perform operations such inserting attribute on matched nodes. problem having requirement of these templates make copy of element node means calling more once produce 2 copies of node.

the attribute insertion template written this:

<xsl:template match = "do_not_match" name = "insertattribute">     <xsl:param name = "attributename"/>     <xsl:param name = "attributevalue"/>     <xsl:element name = "{name(.)}">         <xsl:copy-of select = "@*"/>         <xsl:attribute name = "{$attributename}">             <xsl:value-of select = "$attributevalue"/>         </xsl:attribute>         <xsl:apply-templates />     </xsl:element> </xsl:template> 

i calling template so:

<xsl:template match="object">     <xsl:call-template name = "insertattribute">         <xsl:with-param name = "attributename" select = '"newattribute1"'/>         <xsl:with-param name = "attributevalue" select = '4'/>     </xsl:call-template>     <xsl:call-template name = "insertattribute">         <xsl:with-param name = "attributename" select = '"newattribute2"'/>         <xsl:with-param name = "attributevalue" select = '6'/>     </xsl:call-template> </xsl:template> 

this transform xml document following:

<root>     <object/> </root> 

...into this:

<root>         <object newattribute1="4"/>     <object newattribute2="6"/> </root> 

i'm looking way below output without having use 2 different stylesheets in succession or manually combining 2 template calls single operation. of know way this?

<root>         <object newattribute1="4" newattribute2="6"/> </root> 

i move object creation , attribute processing other template , create new attributes in named template:

<xsl:template match = "do_not_match" name = "insertattribute">     <xsl:param name = "attributename"/>     <xsl:param name = "attributevalue"/>     <xsl:attribute name = "{$attributename}">         <xsl:value-of select = "$attributevalue"/>     </xsl:attribute> </xsl:template>   <xsl:template match="object">   <xsl:copy>     <xsl:copy-of select="@*"/>     <xsl:call-template name = "insertattribute">         <xsl:with-param name = "attributename" select = '"newattribute1"'/>         <xsl:with-param name = "attributevalue" select = '4'/>     </xsl:call-template>     <xsl:call-template name = "insertattribute">         <xsl:with-param name = "attributename" select = '"newattribute2"'/>         <xsl:with-param name = "attributevalue" select = '6'/>     </xsl:call-template>     <xsl:apply-templates/>   </xsl:copy> </xsl:template> 

Comments