Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting every other node uing XPATH

Tags:

xpath

Given an arbitrary amount nodes to select:

<root>
  <foo>1</foo>
  <foo>2</foo>
  <foo>3</foo>
  <foo>4</foo>
  <!-- ... -->
<root>

How do I select every other foo so that I get foo[1], foo[3], ... ?

like image 862
Kit Sunde Avatar asked Nov 23 '10 19:11

Kit Sunde


People also ask

What does node () do in XPath?

node() matches any node (the least specific node test of them all) text() matches text nodes only. comment() matches comment nodes. * matches any element node.

How do you select the second element with the same XPath?

For example if both text fields have //input[@id='something'] then you can edit the first field xpath as (//input[@id='something'])[1] and the second field's xpath as (//input[@id='something'])[2] in object repository.


1 Answers

Try

/root/foo[position() mod 2 = 1]

No idea if it will work right, might need 0. I forget if position starts at 0 or 1

like image 138
Paul Avatar answered Sep 28 '22 19:09

Paul