“//"之间的区别和“/";在 XPath 中? [英] Difference between "//" and "/" in 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'.
这个表达
This expression
//webengagedata//preceding-sibling::*
返回 14 个结果
还有这个表情
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,
/r/preceding-sibling::*
什么都不选,因为r
没有前面的兄弟姐妹./r//preceding-sibling::*
选择前面的兄弟元素r
的所有后代或自 节点.即,a
、b
、c
和d
.(记住,/r//preceding-sibling::*
是/descendant-or-self::node()/preceding-sibling::*
的缩写,not/descendant-or-self::*/preceding-sibling::*
) 注意即使b
和d
是没有元素的前辈兄弟,它们是文本节点的前辈兄弟,因为上面的XML在b之后有空格code> 和
d
.如果删除所有空格,则只会选择a
和c
./r/descendant::*/preceding-sibling::*
选择r
的所有后代 elements 的前面同级元素.即,a
和c
.请注意,b
和d
未被选中,因为它们不在r
的任何后代 元素 之前的同级元素 -- 与前面的示例不同,text 节点不符合条件.
/r/preceding-sibling::*
selects nothing becauser
has no preceding siblings./r//preceding-sibling::*
selects the preceding siblings elements of all of the descendant or self nodes ofr
. That is,a
,b
,c
andd
. (Remember,/r//preceding-sibling::*
is short for/descendant-or-self::node()/preceding-sibling::*
, not/descendant-or-self::*/preceding-sibling::*
) Note that even thoughb
andd
are predecessor siblings to no elements, they are predecessor siblings to text nodes because the above XML has whitespace afterb
andd
. If all whitespace were removed, then onlya
andc
would be selected./r/descendant::*/preceding-sibling::*
selects the preceding sibling elements of all descendant elements ofr
. That is,a
andc
. Note thatb
andd
are not selected because they are not preceding sibling elements to any descendant elements ofr
-- unlike the previous example, text nodes do not qualify.
这篇关于“//"之间的区别和“/";在 XPath 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!