I have xml like follows,
<doc>
<a ref="style1"><b>Test1</b></a>
<a ref="style1"><b>Test2</b></a>
<a ref="style2"><b>Test3</b></a>
</doc>
I need to add new attribute and new node inside <a>
nodes which have attribute "style1"
.
So I wrote following xsl,
//add attribute
<xsl:template match="a[@ref='style1']">
<xsl:copy>
<xsl:attribute name="id">myId</xsl:attribute>
</xsl:copy>
</xsl:template>
//add new node
<xsl:template match="a[@ref='style1']" priority="1">
<xsl:copy>
<newNode></newNode>
</xsl:copy>
<xsl:next-match/>
</xsl:template>
I need to create two templates as above (requirement).
but my current output as follows,
<doc>
<a><newNode/></a><a id="myId"/>
<a><newNode/></a><a id="myId"/>
<a ref="style2"><b>Test2</b></a>
</doc>
as you can see, <a>
nodes has doubled. and <b>
nodes have gone. but m expected output is this,
<doc>
<a id="myId"><newNode/><b>Test1</b>/a>
<a id="myId"><newNode/><b>Test2</b>/a>
<a ref="style2"><b>Test3</b></a>
</doc>
How can I organize my code to get above expected output??