tags:

views:

343

answers:

2

We are using XPath in a Java app and I was wondering: How do I select a set of nodes where the "terminating point" is a node not belonging to the same kind as it's siblings.

For example, I want to get two sets of <a> tags of size 3 and 2 from the example below:

<sample>
   <a />
   <a />
   <a />
   <terminating />
   <a />
   <a />   
</sample>
+1  A: 

First of all, the result of an XPath expression is always either an atomic value, or a single node-set (or sequence in XPath 2.0). You cannot get a list of node-sets.

That said, for your specific example with just two groups and one terminator, you can just use preceding-sibling and following-sibling:

/sample/terminating/preceding-sibling::a
/sample/terminating/following-sibling::a
Pavel Minaev
A: 
Tomalak
I think it could be done even easier - given `$i`, it would be just `/sample/a[count(preceding-sibling::terminating) = $i - 1]` to get all `a` in `$i`th group.
Pavel Minaev
You are right, this would give the same result. Complexity is about the same, though.
Tomalak