xpathgenshi

Match a certain subset of nodes by (global) position number


Is there an XPath syntax to match, for instance, the occurrences numbered 2,3,5,7,11,13 of a certain kind of node? That is, the same result as the union of

//item[2]
//item[3]
//item[5]
...

but in a single expression.

(Use case: I am using a Genshi transformer to match and remove a set of nodes. I can't match and remove them in successive expressions, because their indices would change inbetween.)


Solution

  • You can try using XPath position() like for example :

    //item[position()=2 or position()=3 or position()=5 ...]
    

    or maybe using parentheses if I understand correctly what you mean by "global position number" :

    (//item)[position()=2 or position()=3 or position()=5 ...]