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?
you can use this:
/root/a/b[position()=$variable]
position() is 1 based
http://saxon.sourceforge.net/saxon6.5.3/expressions.html
The following should work:
/root/a/b[2]
And if it doesn't, try:
/root/a/b[position()=2]
position()
refers to the position in the dom [2] refers to the second result in the list of results.
for-each
loop select
statement, they are the same. When position()
is used within the loop itself, it refers to the result set. The [n]
format only works as a shorthand because it is the only conditional inside the [ ]
block, otherwise position()
must be used, as in //a[(@id="xx") and (position()=3)]
, which is "any fifth link that has an id
of xx".
\\*[2]
and \\*[position()=2]
against the question's XML. Thank you!
Success story sharing