[英]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 的这两种变体。
This expression这个表情
//webengagedata//preceding-sibling::*
returned 14 results返回 14 个结果
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,
/r/preceding-sibling::*
selects nothing because r
has no preceding siblings. /r/preceding-sibling::*
选择任何东西,因为r
没有前面的兄弟姐妹。/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
.a
、 b
、 c
和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
. /r//preceding-sibling::*
是/descendant-or-self::node()/preceding-sibling::*
缩写,而不是/descendant-or-self::*/preceding-sibling::*
) 请注意,尽管b
和d
是没有元素的前辈兄弟姐妹,但它们是文本节点的前辈兄弟姐妹,因为上述 XML 在b
和d
之后有空格。 If all whitespace were removed, then only a
and c
would be selected.a
和c
。/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
.a
和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.b
和d
未被选中,因为它们不在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//y
是x/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.