Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between child, following and descendant in XPath axes

Tags:

html

xml

xpath

Till now what all I understand is

  1. ::child looks for immediate child notes of the current node
  2. ::following looks for immediate child and sub child and so on of the current node.
  3. What is ::descendant then?

Can anyone help me understand with simple example?

like image 754
Pratiksha Jadhav Avatar asked Apr 22 '18 12:04

Pratiksha Jadhav


People also ask

What does descendant mean in XPath?

The descendant axis indicates all of the children of the context node, and all of their children, and so forth. Attribute and namespace nodes are not included - the parent of an attribute node is an element node, but attribute nodes are not the children of their parents.

What is the difference between following and following-sibling in Selenium?

following-sibling gets you only the children of the same parent as the current node; following gets all those plus their descendents.

What is the difference between preceding and ancestor in XPath?

The preceding axis selects all nodes that come before the current node in the document, except ancestor, attribute nodes, and namespace nodes.


1 Answers

  • child:: will select the immediate descendants of the context node, but does not go any deeper, like descendant:: does.
  • following:: will select all of the nodes that come after the context node and their descendant's, but that does not include the context node's descendants.
  • descendant:: will select all of the nodes along the child:: axis, as well as their children, and their children's children, etc..

Sometimes, a picture is worth a thousand words:

visualization of XPath axes

Image source, see Figure 3.5

It might be helpful to play with an XPath visualization tool, in order to evaluate your XPath expressions against some sample XML and see what is, and what is not, selected.

For example: http://chris.photobooks.com/xml

like image 105
Mads Hansen Avatar answered Sep 28 '22 02:09

Mads Hansen