ola_user
ola_user

Reputation:

select node ranges using XPath

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>

Upvotes: 0

Views: 985

Answers (2)

Tomalak
Tomalak

Reputation: 338316

Extending on Pavel Minaev's answer - I would write a loop, like this ("i" being the loop index):

  • find all the <terminating> nodes with the help of
    /sample/terminating
  • for each of them, find all the
    ./preceding-sibling::a[count(preceding-sibling::terminating) = {i}]
  • for the last <terminating> node, also find the
    ./following-sibling::a

Upvotes: 0

Pavel Minaev
Pavel Minaev

Reputation: 101615

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

Upvotes: 1

Related Questions