Get Nth child of a node using xpath

XmlXpath

Xml Problem Overview


My sample input XML is:

<root>
 <a>
   <b>item</b>
   <b>item1</b>
   <b>item2</b>
   <b>item3</b>
   <b>item4</b>
 </a>
</root>

I am suppose to select a node b whose position is the value of a variable.

How can I use the value of a variable to test the position of a node?

Xml Solutions


Solution 1 - Xml

you can use this:

/root/a/b[position()=$variable]

position() is 1 based

http://saxon.sourceforge.net/saxon6.5.3/expressions.html

Solution 2 - Xml

The following should work:

/root/a/b[2]

And if it doesn't, try:

/root/a/b[position()=2]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIordanTanevView Question on Stackoverflow
Solution 1 - Xmlremi bourgarelView Answer on Stackoverflow
Solution 2 - XmlRonald WildenbergView Answer on Stackoverflow