如何使用XPath选择除单个节点之外的所有文本?

时间:2018-08-12 13:41:09

标签: html xml xpath

给出以下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>

我希望有一个XPATH可以选择所有不在类c下的文本。

预期输出:

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

1 个答案:

答案 0 :(得分:1)

此XPath,

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

将选择所有没有div的{​​{1}}父节点的文本节点:

@class="c"

如果您要排除纯空格文本节点,则此XPath,

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

some more text i wish to see..

将选择这些文本节点,

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

根据要求。