[英]What is the difference between // and .// in XPath?
When I execute these XPath expression on Chrome Developer Tools' console over google.com, I got the same results当我通过 google.com 在 Chrome 开发者工具的控制台上执行这些 XPath 表达式时,我得到了相同的结果
$x("(.//*[@id='gs_lc0'])")
$x("(//*[@id='gs_lc0'])")
What is the usage of dot in XPath? XPath中点的用法是什么?
In XPath, //
and .//
are both syntactic abbreviations:在 XPath 中,
//
和.//
都是语法缩写:
//
is short for /descendant-or-self::node()/
//
是/descendant-or-self::node()/
缩写.//
is short for self::node()/descendant-or-self::node()/
.//
是self::node()/descendant-or-self::node()/
缩写The descendant-or-self
axis contains the context node and all descendents of the context node. descendant-or-self
轴包含上下文节点和上下文节点的所有后代。 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.在 Google Developer Tools 控制台的顶级评估中,上下文节点是根节点,因此您会看到相同的结果。
In short:简而言之:
//
when you wish to select nodes from the entire document.//
。.//
when you wish to select nodes only beneath the context node..//
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.