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

不打扰是莪最后的温柔 提交于 2019-12-11 04:47:57

问题


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


来源:https://stackoverflow.com/questions/39902960/what-is-the-difference-between-and-in-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!