While working with XML in some of the cases, we want to get the index or position of particular value. This can be easily done by using XSLT.
In this article, I am explaining - How to get index of node based on node value using XSLT?
Suppose, I have this XML data-
Now if I want to get the position of ‘A’ then It should return '2' .
How to get it-
This is small XSLT code to get this position-
<!--Declaring Variable with value-->
<xsl:variable name="TempData" select="A"/>
<!--XSLT code to get position of 'A', output will be 2 -->
<xsl:value-of select="count(//abc:ResponseData/abc:value[text()=$TempData]/preceding-sibling::*)+1" />
In above line I have declared one variable - 'TempData'
and using select I have assigned value -'A'
Now to get the position of 'A', I have used count() method.
Here this expression -
Will return the count of preceding sibling of A, which will be position of 'A'.
Like 'A', we can also get the position of other Values ('B' and 'C'), for this we need to assign this value to 'TempData' variable.