Difference between child, following and descendant in XPath axes

10,634

Solution 1

  • 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

Solution 2

XPath Axis Family Tree Analogy

The major XPath axes follow family tree terminology:

  • self:: is you.

Downward:

  • child:: are your immediate children.
  • descendant:: are your children, and their children, recursively.
  • descendant-or-self:: (aka //): are you and your descendants.

Upward:

  • parent:: is your mother or father.1
  • ancestor:: are your parent, and your parent's parent, recursively.
  • ancestor-or-self:: are you and your ancestors.

Sideways (consider elements earlier in the document to be younger):

  • previous-sibling:: are your younger siblings, in age order.
  • following-sibling:: are your older siblings, in age order.
  • previous:: are your younger siblings and their descendants, in age order.
  • following:: are your older siblings and their descendants, in age order.

1Not both, because XML elements have only a single parent.

Solution 3

XPath Axes

  • We will discuss the following terms with respect to the following HTML :

<?xml version="1.0" encoding="UTF-8"?>
<head>
	<meta content="image" property="my_property">
</head>
<body>
	<bookstore>

	<book>
	  <title lang="en">Harry Potter</title>
	  <price>29.99</price>
	</book>

	<book>
	  <title lang="en">Learning XML</title>
	  <price>39.95</price>
	</book>

	</bookstore>
</body>
  • child : Selects all children of the current node. For example, if your current node is <book>, the keyword child will select both the nodes :

    <title lang="en">Harry Potter</title>
    <price>29.99</price>        
    
  • following : Selects everything in the document after the closing tag of the current node. For example, if your current node is <head>, the keyword following will select all the nodes :

    <body>
        <bookstore>
    
        <book>
          <title lang="en">Harry Potter</title>
          <price>29.99</price>
        </book>
    
        <book>
          <title lang="en">Learning XML</title>
          <price>39.95</price>
        </book>
    
        </bookstore>
    </body>
    
  • descendant : Selects all descendants (children, grandchildren, etc.) of the current node. For example, if your current node is <body>, the keyword descendant will select all the nodes :

    <bookstore>
    
    <book>
      <title lang="en">Harry Potter</title>
      <price>29.99</price>
    </book>
    
    <book>
      <title lang="en">Learning XML</title>
      <price>39.95</price>
    </book>
    
    </bookstore>
    

Solution 4

Assume each nodes as box. now in an html page we have many boxes and each boxes other small boxes in it.

now when we say: child:: of a box - this would mean all the boxes inside the main box that we are looking at. this will only consider the boxes inside the current box. It won't look at the contents of the boxes.

following:: of a box - this would mean all the boxes after the box that I am looking at, this doesn't have anything to do with the current box that I am looking at.

descendant: it is like child, but the catch is - it looks all the boxes inside the node box and also look inside of each of the sub boxes too. child will only look for immediate boxes not inside each of the immediate boxes.

Share:
10,634
Pratiksha Jadhav
Author by

Pratiksha Jadhav

Updated on July 22, 2022

Comments

  • Pratiksha Jadhav
    Pratiksha Jadhav almost 2 years

    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?

  • Pratiksha Jadhav
    Pratiksha Jadhav about 6 years
    Thank you so much! Image is bonus :-)
  • Pratiksha Jadhav
    Pratiksha Jadhav about 6 years
    Thanks for the explanation with example! It really helped
  • Pratiksha Jadhav
    Pratiksha Jadhav about 6 years
    Thanks for the simple word answer!
  • JeffC
    JeffC about 6 years
    Hopefully not too pedantic but shouldn't "child:: will select the immediate children"... be " child:: will select the immediate descendants". There are no children other than immediate children. Where did you find the image? I like it and I'm gonna steal it as a reference. Thanks for the link also... I think I like that tool better than the one I've been using.
  • Mads Hansen
    Mads Hansen about 6 years
    Yes, immediate descendants would be more precise. I’ll update. The image should have been sourced. It was referenced in this article informit.com/articles/article.aspx?p=29844&seqNum=3:
  • JeffC
    JeffC about 6 years
    Your example HTML is not HTML, it's XML and it's not valid XML. child would select both sets of children (2 title and 2 price) since there are two book tags. //following::head pulls only the head tag, open to close tag, and nothing else.
  • undetected Selenium
    undetected Selenium about 6 years
    @JeffC Perhaps your understanding is not clear about xpaths and you can always raise a ticket to get help. Stackoverflow volunteers will be happy to help you out.
  • JeffC
    JeffC about 6 years
    No, what I stated is the result of my testing. If you think otherwise, post the XPath you used to get the results you listed.