battleck
battleck

Reputation: 13

Why is this XPath test against child text contents failing?

Here is my example XML document:

<div class="firstlevel" id="firstid">
    <div class="secondlevel">
        <span class="thirdlevel">
            <a href="somelinknew">111 new</a>
            <span class="fourthlevel">222</span>
        </span>
        <span class="thirdlevel">
            <a href="somelinkold">333 old</a>
            <span class="fourthlevel">444</span>
        </span>
    </div>
</div>

I have tried this XPath:

//*[@class='thirdlevel' and //text()[contains(.,'new')]]/span

But it returns both values, 222 and 444. Why? I think the XPath is looking for "new" but show return also "old"?!

The main problem is, the code can also be like this:

<div class="firstlevel" id="firstid">
    <div class="secondlevel">
        <span class="thirdlevel">
            <a href="somelinkold">333 old</a>
            <span class="fourthlevel">444</span>
        </span>
    </div>
</div>

or like this:

<div class="firstlevel" id="firstid">
    <div class="secondlevel">
        <span class="thirdlevel">
            <a href="somelinknew">111 new</a>
            <span class="fourthlevel">222</span>
        </span>
    </div>
</div>

or just like this:

<div class="firstlevel" id="firstid">
    <div class="secondlevel">
    </div>
</div>

Upvotes: 1

Views: 27

Answers (1)

kjhughes
kjhughes

Reputation: 111491

//text() is checking all text nodes in the document because // is checking self or descendents from the root level down. Instead, just look for 'new' in the text starting from the current node, ., and descendents from there:

//*[@class='thirdlevel' and .//text()[contains(.,'new')]]/span

which can be simplified to

//*[@class='thirdlevel' and contains(.,'new')]/span

Upvotes: 3

Related Questions