I am writing xpath code but I do not know the difference, when I use //li[div]//a[2] shows no results found. link: https://www.yelp.nl/search?find_desc=spa&find_loc=der%20N%C3%A4he%20von%20California%2C%20Louisville%2C%20KY%2C%20Vereinigte%20Staaten&start=70
Asked
Active
Viewed 140 times
1 Answers
3
//
is a shorter way of writing /descendant-or-self::node()/
.
In some structures, it can return different nodes:
<ul>
<li>
<div><b>
<a id="1"></a>
<a id="2"></a>
<p>
<a id="3"></a>
<a id="4"></a>
</p>
</b></div>
</li>
<li>
<div><b>
<a id="5"></a>
<a id="6"></a>
<p>
<a id="7"></a>
<a id="8"></a>
</p>
</b></div>
</li>
</ul>
Now, only ids 2 and 6 correspond to //li[div]/descendant::a[2]
,
but //li[div]//a[2]
matches 2, 4, 6, and 8.
See Abbreviated Syntax in the XML Path Language (XPath) specification.

choroba
- 231,213
- 25
- 204
- 289
-
https://stackoverflow.com/a/36020488/1405588 says `descendant-or-self::` - which is correct? – o11c Aug 31 '20 at 23:07
-
Neither, sorry. I fixed my answer. – choroba Aug 31 '20 at 23:18
-
Neither, really? If you're saying some part of [stackoverflow.com/a/36020488/1405588](https://stackoverflow.com/a/36020488/290085) is incorrect, please specify. – kjhughes Sep 01 '20 at 04:56
-
@kjhughes: It's neither `/descendant::` nor `/descendant-or-self::`, it's `/descendant-or-self::node()/`. – choroba Sep 01 '20 at 07:18
-
The linked answer (which I wrote) does say `/descendant-or-self::node()/`, so it is correct. @o11c was also correct to point out your initial mistake, which was that you got the *axis* wrong. @o11c provided you with the correct axis and a correct reference, not the full expansion of the `//` abbreviation. His comment and his referenced answer were both on the mark and helpful. – kjhughes Sep 01 '20 at 12:44
-
Beyond the obvious expansion of `//` difference, the key difference between those two XPaths is how `[2]` is interpreted differently along those two axis. See duplicate link for further details on this difference. Thank you. – kjhughes Sep 01 '20 at 12:48
-
Yes, the link was helpful. The "says `descendant-or-self::`" was the part I replied to. – choroba Sep 01 '20 at 13:39
-
Ok, thanks for the clarification. +1 – kjhughes Sep 01 '20 at 14:29