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

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

Leave a Comment