Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meaning of .// in XPath?

Tags:

xml

xpath

I know absolute XPath will return the inspected node from root node in XML tree.

But I am not able to understand the meaning of .// used in XPath to inspect/find a node.

like image 913
TodayILearned Avatar asked Jul 13 '15 03:07

TodayILearned


People also ask

What is the meaning of '/' in XPath?

0 votes. Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

What is the difference between '/' and in XPath?

Difference between “/” and “//” in XPathSingle slash is used to create absolute XPath whereas Double slash is used to create relative XPath. 2. Single slash selects an element from the root node. For example, /html will select the root HTML element.

What is the * indicates in XPath?

By adding '//*' in XPath you would be selecting all the element nodes from the entire document. In case of the Gmail Password fields, .//*[@id='Passwd'] would select all the element nodes descending from the current node for which @id-attribute-value is equal to 'Passwd'.

Which symbols are used in XPath?

It starts with the forward slash '/' . Generally, Absolute XPath is not recommended because in future any of the web element when added or removed then Absolute XPath changes. Relative XPath; In this, XPath begins with the double forward slash '//' which means it can search the element anywhere in the Webpage.


1 Answers

. is the current node; it is short for self::node().

// is the descendant-or-self axis; it is short for /descendant-or-self::node()/.

Together, .// will select along the descendent-or-self axis starting from the current node. Contrast this with // which starts at the document root.

Example

Consider the following HTML:

<html>
  <body>
    <div id="id1">
      <p>First paragraph</p>
      <div>
        <p>Second paragraph</p>
      </div>
    </div>
    <p>Third paragraph</p>
  </body>
</html>

//p will select all paragraphs:

      <p>First paragraph</p>
      <p>Second paragraph</p>
      <p>Third paragraph</p>

On the other hand, if the current node is at the div element (with @id of "id1"), then .//p will select only the paragraphs under the current node:

      <p>First paragraph</p>
      <p>Second paragraph</p>

Notice that the third paragraph is not selected by .//p when the current node is the id1 div because the third paragraph is not under that div element.

like image 93
kjhughes Avatar answered Sep 20 '22 13:09

kjhughes