TodayILearned
TodayILearned

Reputation: 1500

What is meaning of .// in 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.

Upvotes: 14

Views: 9396

Answers (1)

kjhughes
kjhughes

Reputation: 111541

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

Upvotes: 19

Related Questions