Difference between "//" and "/" in XPath?
/
vs //
in general
Both child
(/
) and descendant-or-self
(//
) are axes in XPath.
/
is short for/child::node()/
.Use
/
to select a node's immediate children.//
is short for/descendant-or-self::node()/
.Use
//
to selecta node, its children, its grandchildren, and so onrecursively.
/
vs //
with preceding-sibling::*
Your specific question asks about the difference between //preceding-sibling::*
and /preceding-sibling::*
.
Since your data is offsite and complex, let's consider instead this present and simpler XML:
<r> <a/> <b> <c/> <d/> </b></r>
For this XML,
/r/preceding-sibling::*
selects nothing becauser
has nopreceding siblings./r//preceding-sibling::*
selects the preceding siblings elements ofall 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.
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
//webengagedata//preceding-sibling::* ---> returned 14 results
Here it is considering child tags as well as biziclop said x/descendant-or-self::node()/y
The difference is that x//y
is shorthand for 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.
The rules of abbreviated xpath syntax are explained here.