简体   繁体   中英

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM