2

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

jasiustasiu
  • 1,468
  • 2
  • 20
  • 28
  • Suggested answer does NOT fit my question. Index of element is dynamic and I needed to convert it to number before – jasiustasiu May 30 '14 at 12:31

2 Answers2

2

Convert the value of the iteration into a number before applying it as an indexer, viz

/xml/myElement[number(/xml/iteration/text())]
StuartLC
  • 104,537
  • 17
  • 209
  • 285
1

There is similar question

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

Community
  • 1
  • 1