Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between //node and /descendant::node in xpath?

Tags:

xpath

I use a lot of XPath when locating elements in web pages using Selenium, and have moved away from using node1//node2 towards using node1/descendant::node2 more recently. What's the difference between the two methods? Is one more efficient than the other?

Example XML snippet to demonstrate:

<div id="books">   <table>     <tr><td class="title">Lord of the Rings</td><td class="author">JRR Tolkein</td></tr>     <tr><td class="title">The Hitch-Hikers Guide to the Galaxy</td><td class="author">Douglas Adams</td></tr>   </table> </div> 

So it'd be:

id('books')//td[@class='title']

or:

id('books')/descendant::td[@class='title']
like image 836
Dave Hunt Avatar asked Oct 08 '09 13:10

Dave Hunt


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 difference between descendant and following in XPath?

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..

How many types of nodes are there in XPath specification?

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.

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

see http://www.w3.org/TR/xpath#path-abbrev

// is just an abbreviation for the descendant:: axis

Edit

To quote:

//para is short for /descendant-or-self::node()/child::para

That is, it refers to all para which are a child of the context node or any node descended from the context node. As far as I can tell that translates into any descendant para of the context node.

like image 116
Jonathan Fingland Avatar answered Sep 20 '22 09:09

Jonathan Fingland