jasiustasiu
jasiustasiu

Reputation: 1668

How to use XPath expression to find element in an array

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

Answers (2)

Iaroslav Karandashev
Iaroslav Karandashev

Reputation: 574

There is similar question

Could you try this one: /xml/myElement[position()=/xml/iteration/text()] ?

Upvotes: 1

StuartLC
StuartLC

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

Related Questions