简体   繁体   English

XPath 中“//”和“/”的区别?

[英]Difference between “//” and “/” in XPath?

I was trying my hands on XPath for python-selenium.我正在尝试使用 XPath for python-selenium。

I used this link for trying some XPaths' from tutorials:我使用此链接从教程中尝试了一些 XPath:

So I tried these two variants of XPaths'.所以我尝试了 XPaths 的这两种变体。

  1. This expression这个表情

    //webengagedata//preceding-sibling::*

returned 14 results返回 14 个结果

  1. And this expression还有这个表情

    //webengagedata/preceding-sibling::*

returned 9 results返回 9 个结果

What does the "//" do to match 5 more results? “//”如何匹配另外 5 个结果?

/ vs // in general / vs //一般情况下

Both child ( / ) and descendant-or-self ( // ) are axes in XPath . child ( / ) 和descendant-or-self ( // ) 都是XPath中的

  • / is short for /child::node()/ . //child::node()/缩写。

    Use / to select a node's immediate children .使用/选择节点的直接子节点。

  • // is short for /descendant-or-self::node()/ . ///descendant-or-self::node()/缩写。

    Use // to select a node, its children, its grandchildren, and so on recursively.使用//递归地选择一个节点、它的子节点、它的孙节点等等。


/ vs // with preceding-sibling::* / vs //带有preceding-sibling::*

Your specific question asks about the difference between //preceding-sibling::* and /preceding-sibling::* .您的具体问题询问了//preceding-sibling::*/preceding-sibling::*之间的区别。

Since your data is offsite and complex, let's consider instead this present and simpler XML:由于您的数据是异地且复杂的,让我们考虑一下这个现有且更简单​​的 XML:

<r>
  <a/>
  <b>
    <c/>
    <d/>
  </b>
</r>

For this XML,对于这个 XML,

  1. /r/preceding-sibling::* selects nothing because r has no preceding siblings. /r/preceding-sibling::*选择任何东西,因为r没有前面的兄弟姐妹。
  2. /r//preceding-sibling::* selects the preceding siblings elements of all of the descendant or self nodes of r . /r//preceding-sibling::*选择所有的后代或自节点的前述同级元素r That is, a , b , c and d .即, abcd (Remember, /r//preceding-sibling::* is short for /descendant-or-self::node()/preceding-sibling::* , not /descendant-or-self::*/preceding-sibling::* ) Note that even though b and d are predecessor siblings to no elements , they are predecessor siblings to text nodes because the above XML has whitespace after b and d . (请记住, /r//preceding-sibling::*/descendant-or-self::node()/preceding-sibling::*缩写,而不是/descendant-or-self::*/preceding-sibling::* ) 请注意,尽管bd是没有元素的前辈兄弟姐妹,但它们是文本节点的前辈兄弟姐妹,因为上述 XML 在bd之后有空格。 If all whitespace were removed, then only a and c would be selected.如果删除所有空格,则只会选择ac
  3. /r/descendant::*/preceding-sibling::* selects the preceding sibling elements of all descendant elements of r . /r/descendant::*/preceding-sibling::*选择r的所有后代元素的前面同级元素。 That is, a and c .ac Note that b and d are not selected because they are not preceding sibling elements to any descendant elements of r -- unlike the previous example, text nodes do not qualify.请注意, bd未被选中,因为它们不在r任何后代元素之前的同级元素——与前面的示例不同,文本节点不符合条件。

For your example对于你的例子

//webengagedata/preceding-sibling::* ---> returned 9 results

Because there are only 9 tags which are exact sibling of webengagedata tags thats why it is showing 9 records因为只有 9 个标签与webengagedata标签完全相同,这就是它显示 9 条记录的原因

//webengagedata//preceding-sibling::* ---> returned 14 results

Here it is considering child tags as well as biziclop said x/descendant-or-self::node()/y这里正在考虑子标签以及 biziclop 表示x/descendant-or-self::node()/y

The difference is that x//y is shorthand for x/descendant-or-self::node()/y .不同之处在于x//yx/descendant-or-self::node()/y简写。

That's all.就这样。

So while the first query selects all the descendants of <webengagedata> that have another tag after them, the second only selects the preceding siblings of the tag itself.因此,虽然第一个查询选择<webengagedata>所有后代,但在它们之后有另一个标签,第二个查询只选择标签本身的前一个兄弟。

The rules of abbreviated xpath syntax are explained here .此处解释了缩写 xpath 语法的规则。

xpath中'/'和'//'的区别是,'/'用于标识该区域内的元素,'//'用于条件后的整个页面

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

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