Question regarding the traversing behaviour of preceding axes when used with / and //
<div id="moe-osm-pusher" style="display: block !important; height: 0px;"> <div><p>A44</p></div> </div> <div id="x"> <p>A</p> <div> <div> <p>A11</p> </div> <div> <p>A12</p> <div> <p>A13</p> </div> <div> <p>A23</p> <div><p>A00</p></div> </div> </div> <div> <p>A22</p> </div> </div> </div>
//div[p[contains(text(),'A23')]]// preceding::div
//div[p[contains(text(),'A23')]]/preceding::div
My question is whether first xpath will locate this <div><p>A00</p></div>
or not ?
I saw in http://xpather.com/ it is locating the div but why . Isn't Xpath meant to go backwards.
Both of the xpath common output is :
<div> <p>A13</p></div>
<div> <p>A11</p> </div>
<div><p>A44</p></div>
<div id="moe-osm-pusher" style="display: block !important; height: 0px;"></div>
which is alright.
Answers
-
Double slash will match any descendant. In your example, double slash would match <p>A23<p> and <div><p>A00</p></div> including their text nodes whereas single slash would only match the immediate child element nodes.
My question is whether first xpath will locate this <div><p>A00</p></div> or not ?
No. The "preceding" axes will match previous nodes in the Document. The div containing "A00" is comes after that particular "p" node (containing "A23") and not before. Since it comes after, you could use "following-sibling" instead.
0