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

1
  • Suggested answer does NOT fit my question. Index of element is dynamic and I needed to convert it to number before Commented May 30, 2014 at 12:31

2 Answers 2

2

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

/xml/myElement[number(/xml/iteration/text())]
Sign up to request clarification or add additional context in comments.

Comments

1

There is similar question

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.