Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find sibling node after specified node is found

Tags:

xpath

Consider the following very simplified example.

<n></n> <k></k> <m></m> <k></k> 

How can I search for a first k sibling after m node? Basically, find some node and then continue searching from that node.

like image 206
wpfwannabe Avatar asked Apr 07 '12 14:04

wpfwannabe


People also ask

How can I get next sibling in XPath?

To traverse to the next sibling, we have to use the following-sibling concept in xpath. This will allow us to traverse to the next sibling from the present sibling of the same parent. Let us try to move from the first child<h1> of parent <div> to the second <h2> as in the above image.

What is sibling of a node?

Sibling nodes are nodes on the same hierarchical level under the same parent node. Nodes higher than a given node in the same lineage are ancestors and those below it are descendants.


1 Answers

How can I search for a first k sibling after m node? Basically, find some node and then continue searching from that node.

Assuming that we have the following well-formed XML document:

<t>     <n></n>     <k></k>     <m></m>     <k></k> </t> 

then the following XPath expression:

/*/m[1]/following-sibling::k[1] 

selects the first k following-sibling of the first m child of the top element of the XML document.

like image 144
Dimitre Novatchev Avatar answered Oct 21 '22 20:10

Dimitre Novatchev