“//"之间的区别和“/";在 XPath 中? [英] Difference between "//" and "/" in XPath?

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

问题描述

我正在尝试使用 XPath for python-selenium.

I was trying my hands on XPath for python-selenium.

我使用此链接来尝试教程中的一些 XPath:

I used this link for trying some XPaths' from tutorials:

所以我尝试了 XPaths 的这两种变体.

So I tried these two variants of XPaths'.

  1. 这个表达

  1. This expression

//webengagedata//preceding-sibling::*

返回 14 个结果

  1. 还有这个表情

  1. And this expression

//webengagedata/preceding-sibling::*

返回 9 个结果

//"如何匹配另外 5 个结果?

What does the "//" do to match 5 more results?

推荐答案

/ vs // 一般

child (/) 和 descendant-or-self (//) 都是 XPath 中的轴.

/ vs // in general

Both child (/) and descendant-or-self (//) are axes in XPath.

  • //child::node()/ 的缩写.

使用 / 选择节点的直接子节点.

///descendant-or-self::node()/ 的缩写.

使用//来选择一个节点、它的子节点、它的孙节点等等递归.

Use // to select a node, its children, its grandchildren, and so on recursively.

/ vs //preceding-sibling::*

/ vs // with preceding-sibling::*

您的具体问题询问了 //preceding-sibling::*/preceding-sibling::* 之间的区别.

Your specific question asks about the difference between //preceding-sibling::* and /preceding-sibling::*.

由于您的数据是异地且复杂的,让我们考虑使用这种现有且更简单​​的 XML:

Since your data is offsite and complex, let's consider instead this present and simpler XML:

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

对于这个 XML,

  1. /r/preceding-sibling::* 什么都不选,因为 r 没有前面的兄弟姐妹.
  2. /r//preceding-sibling::* 选择前面的兄弟元素r 的所有后代或自 节点.即,abcd.(记住,/r//preceding-sibling::*/descendant-or-self::node()/preceding-sibling::* 的缩写,not /descendant-or-self::*/preceding-sibling::*) 注意即使 bd是没有元素的前辈兄弟,它们是文本节点的前辈兄弟,因为上面的XML在bd.如果删除所有空格,则只会选择 ac.
  3. /r/descendant::*/preceding-sibling::* 选择 r 的所有后代 elements 的前面同级元素.即,ac.请注意,bd 未被选中,因为它们不在 r 的任何后代 元素 之前的同级元素 -- 与前面的示例不同,text 节点不符合条件.
  1. /r/preceding-sibling::* selects nothing because r has no preceding siblings.
  2. /r//preceding-sibling::* selects the preceding siblings elements of all of the descendant or self nodes of r. That is, a, b, c and d. (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. If all whitespace were removed, then only a and c would be selected.
  3. /r/descendant::*/preceding-sibling::* selects the preceding sibling elements of all descendant elements of r. That is, a and c. 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.

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

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