Selecting every other node uing 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 开发者_JS百科foo[1], foo[3], ... ?
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
position() returns a number that indicates the position of an element relative to other child elements. The mod function returns the „rest“ of a division: 5 mod 2 = 1; 6 mod 2 = 0; 9 mod 2 = 1; 10 mod 2 = 0;
also see: https://en.wiktionary.org/wiki/modulo
精彩评论