Reputation: 1668
I have few elements of the same type in my xml file and node with number of occurence that I need to use eg
<xml>
<iteration>3</iteration>
<myElement>123</myElement>
<myElement>456</myElement>
<myElement>789</myElement>
<myElement>012</myElement>
</xml>
Now I need to select 3rd (text of iteration node) myElement node. How to do that using XPath? I tried
(/xml/myElement)[/xml/iteration/text()]
but it doens't work. Is it possible?
Of course expected result is 789
Upvotes: 2
Views: 7441
Reputation: 574
There is similar question
Could you try this one:
/xml/myElement[position()=/xml/iteration/text()]
?
Upvotes: 1
Reputation: 107317
Convert the value of the iteration into a number
before applying it as an indexer, viz
/xml/myElement[number(/xml/iteration/text())]
Upvotes: 2