【问题标题】:How to select all text excluding a single node using XPath?如何使用 XPath 选择除单个节点之外的所有文本?
【发布时间】:2019-01-19 10:01:38
【问题描述】:

给定以下 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..

【问题讨论】:

  • 分享你当前的 XPath

标签: html xml xpath


【解决方案1】:

这个 XPath,

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

将选择所有没有 div 父级 @class="c" 的文本节点:

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

some more text i wish to see..

如果你想排除只有空白的文本节点,那么这个 XPath,

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

将选择这些文本节点,

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

根据要求。

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多