Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Question regarding the traversing behaviour of preceding axes when used with / and //

Options
JIT
JIT Posts: 1
<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.

Tagged:

Answers

  • benken_parasoft
    benken_parasoft Posts: 1,235 ✭✭✭
    edited April 22
    Options

    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.

Tagged