Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining the use of preceding and following sibling in the same xpath query

Tags:

I have a quite simple problem but i can't seem to resolve it. Let's say i have the following code:

<a>     <b property="p1">zyx</b>     <b>wvu</b>     <b>tsr</b>     <b property="p2">qpo</b>     <b>qcs</b> </a> 

I want to select the nodes between the b node who has a property="p1" and the b node who has property="p2". I can do either of those with the preceding-sibling and the following-sibling axis but I can't seem to find how to combine both.

like image 431
raph.amiard Avatar asked Aug 02 '10 14:08

raph.amiard


People also ask

What is preceding sibling and following-sibling?

This is a concept very similar to Following-Siblings. The only difference in functionality is that of preceding. So, here, in contrast to Following-Sibling, you get all the nodes that are siblings or at the same level but are before your current node.

What is preceding and following xpath?

The following-sibling and preceding-sibling axes contain the siblings before or after the context node, and the following and preceding axes contain all nodes in the document before or after the context node, but: None of these axes contain attribute or namespace nodes.

What is following-sibling in xpath?

The following-sibling axis indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.


1 Answers

XPath 1.0:

/a/b[preceding-sibling::b/@property='p1' and following-sibling::b/@property='p2'] 

XPath 2.0:
The expression above has some quirks in XSLT 2.0, it is better to use the new and safer operators << (before) and >> (after).

/a/b[../b[@property='p2'] << . and . >> ../b[@property='p1']] 
like image 72
Dimitre Novatchev Avatar answered Sep 26 '22 01:09

Dimitre Novatchev