在XPath中,//和.//有什么区别? [英] What is the difference between // and .// in XPath?

查看:113
本文介绍了在XPath中,//和.//有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过google.com在Chrome开发人员工具的控制台上执行这些XPath表达式时,我得到了相同的结果


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


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




XPath中dot的用法是什么?

在bash中, // .// 都是语法缩写:


  • // / descendant-or-self :: node()/

  • .// self :: node()/ descendant-or-self :: node ()/



descendant-or-self axis包含上下文节点和上下文节点的所有后代。因此, // .// 之间的区别在上下文节点中减少了。

对于 // ,上下文节点是根节点; // 绝对位置路径

对于 .// ,上下文节点取决于上下文; .// 是一个相对位置路径的。在Google Developer Tools控制台的顶级评估中,上下文节点是根节点,因此您会看到相同的结果。



简而言之:




  • 当您希望从整个节点中选择节点时,使用 // 文档。

  • 当您希望选择仅位于上下文节点下方的节点时,请使用 .//

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?

解决方案

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.

这篇关于在XPath中,//和.//有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆