简体   繁体   English

XPath 中的 // 和 .// 有什么区别?

[英]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:简而言之:

  • Use // when you wish to select nodes from the entire document.当您希望从整个文档中选择节点时,请使用//
  • Use .// when you wish to select nodes only beneath the context node.当您希望仅选择上下文节点下方的节点时,请使用.//

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM