Shai Ben-Dor
Shai Ben-Dor

Reputation: 573

How to select all text excluding a single node using XPath?

Given the following XML:

<div class="a">
    some text i want to see
    <div class="b">
        other text i want to see
    <div>
    <div class="c">
        some text i DON'T WANT to see
    </div>  
    some more text i wish to see..
</div>

I would like to have an XPATH that selects all the text that is not under class c.

Expected output:

some text i want to see
other text i want to see
some more text i wish to see..

Upvotes: 0

Views: 50

Answers (1)

kjhughes
kjhughes

Reputation: 111491

This XPath,

//div[@class="a"]//text()[not(parent::div[@class="c"])]

will select all text nodes without a div parent of @class="c":

some text i want to see
other text i want to see

some more text i wish to see..

If you want to exclude white-space-only text nodes, then this XPath,

//div[@class="a"]//text()[not(parent::div[@class="c"]) and normalize-space()]

will select these text nodes,

some text i want to see
other text i want to see
some more text i wish to see..

as requested.

Upvotes: 1

Related Questions