I have xml file like follows,
<doc>
<a ref="Foot"></a>
<a ref="Foot"></a>
<a ref="Foot"></a>
<a ref="End"></a>
<a ref="End"></a>
<a ref="End"></a>
<doc>
my requirement was add dynamically increment id attribute to the <a>
node that have "End"
attribute. and also change "Foot"
attribute with "End"
so the result document would be,
<doc>
<a ref="End" id="1"></a>
<a ref="End" id="2"></a>
<a ref="End" id="3"></a>
<a ref="End" id="4"></a>
<a ref="End" id="5"></a>
<a ref="End" id="6"></a>
<doc>
I was able to add dynamic id to nodes and change the "Foot" attribute with "End" but the id's only added to the node which previously have "End" attribute. nodes which have attribute "Foot" do not add the id. My current output is follows,
<doc>
<a ref="End"></a>
<a ref="End"></a>
<a ref="End"></a>
<a ref="End" id="1"></a>
<a ref="End" id="2"></a>
<a ref="End" id="3"></a>
<doc>
My xsl code is follows,
//adds dynamic id's to foot node
<xsl:template match="a/@ref[.='End']">
<xsl:attribute name="id">
<xsl:number count="a[@ref='End']" level="any"></xsl:number>
</xsl:attribute>
</xsl:template>
//change attribute "Foot" to attribute "End"
<xsl:template match="a/@ref[. = 'Foot']">
<xsl:attribute name="id">End</xsl:attribute>
</xsl:template>
My problem is how can add id's to first three nodes. I may could use xsl variables but I'm new to xslt and i couldn't thik of a way how can I use variables. also if we can do the "Foot" attribute to "End" attribute conversation first and then add the id's then code will run fine. I also have no idea of is it possible with xslt.
can anyone suggest me an answer how can I do this?
Thanks in advance.
<a ref="Foot"></a>
I don't get why you writematch="a[. = 'Foot']"
and not<xsl:template match="a/@ref[. = 'Foot']"><xsl:attribute name="ref">End</xsl:attribute>
? – Martin Honnena
elements whoseref
attribute is neither "Foot" nor "End"? If yes, how should they be handled? – michael.hor257k