Reputation: 185
Till now what all I understand is
::child
looks for immediate child notes of the current node::following
looks for immediate child and sub child and so on of the current node.::descendant
then?Can anyone help me understand with simple example?
Upvotes: 14
Views: 28351
Reputation: 51
Child tag will only identify the child of the current node if the current node has a grandchild in it, it won't scan it, whereas the descendant tag will scan the grad child from the current node
Upvotes: 0
Reputation: 66724
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:
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
Upvotes: 23
Reputation: 111531
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.1ancestor::
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.
Upvotes: 11
Reputation: 406
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.
Upvotes: 2
Reputation: 193088
<?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>
Upvotes: 3