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

69
January 19, 2021, at 6:40 PM

When I execute these XPath expression on Chrome Developer Tools' console over google.com, I got the same results

  • $x("(.//*[@id='gs_lc0'])")

  • $x("(//*[@id='gs_lc0'])")

What is the usage of dot in XPath?

Answer 1

In XPath, // and .// are both syntactic abbreviations:

  • // is short for /descendant-or-self::node()/
  • .// is short for self::node()/descendant-or-self::node()/

The descendant-or-self axis contains the context node and all descendents of the context node. So the difference between // and .// is reduces to a difference in context nodes.

For //, the context node is the root node; // is an absolute location path.

For .//, the context node depends upon the context; .// is a relative location path. At the top-level evaluation in Google Developer Tools console, the context node is the root node, so you'll see identical results.

In short:

  • Use // when you wish to select nodes from the entire document.
  • Use .// when you wish to select nodes only beneath the context node.
READ ALSO
NodeJS checking if XML element exists and add or delete

NodeJS checking if XML element exists and add or delete

I'm working with XML in NodeJSI've been using the xmlbuilder to create my XML

57
Extracting array value from nested array of objects

Extracting array value from nested array of objects

Hi guys i have created the following array of objectsAnd to be honest i am a little bit lost

97
How to use CloudFireStore on Android Stuido

How to use CloudFireStore on Android Stuido

I want to use CloudFireStore From Firebase How can I start It on Android Studio Project

71