问题
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?
回答1:
In XPath, //
and .//
are both syntactic abbreviations:
//
is short for/descendant-or-self::node()/
.//
is short forself::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.
来源:https://stackoverflow.com/questions/39902960/what-is-the-difference-between-and-in-xpath