首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于获取省略子节点的主段落文本的Xpath

用于获取省略子节点的主段落文本的Xpath
EN

Stack Overflow用户
提问于 2018-07-08 00:47:24
回答 1查看 55关注 0票数 1

我希望匹配以下代码的主要段落内容,省略子节点p,div,h3。

<div class="content">
    sunday, monday, tuesday,
    <br>
    <br>
    wednesday, thursday,
    <br>
    friday, saturday
    <div class ="tags">sunday</div>
    <h3>Days</h3>
    <p>....</p>
    <div class="style">monday to friday</div>
</div>

我尝试过像//div[@class="content"]/*[not(self::p)]//div[@class="content"]/*[not(name()="p")]这样的Xpath,但它们都不起作用。然后我尝试了//div[@class="content"]/node()[not(div)]//div[@class="content"]/node()[not(h3)],它只匹配第一个文本。

我需要下面的文本

sunday, monday, tuesday,
<br>
<br>
wednesday, thursday,
<br>
friday, saturday

通过省略孩子div class ="tags",h3,p,div class = style。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-08 10:35:47

这应该能起到作用:

//div[@class="content"]/*[not(self::p) and not(self::h3) and not(self::div)]|//div[@class="content"]/text()

sunday, monday, tuesday, wednesday, thursday, friday, saturday

sunday

Days

....

monday to friday

&=

sunday, monday, tuesday, wednesday, thursday, friday, saturday

sunday

Days

....

monday to friday

&extract=//div[@class="content"]/ *[not(self::p) and not(self::h3) and not(self::div[@class="style"]) and not(self::div[@class="tags"])] | //div[@class="content"]/text()&=//div[@class="content"]/ *[not(self::p) and not(self::h3) and not(self::div[@class="style"]) and not(self::div[@class="tags"])] | //div[@class="content"]/text()&input-format=html&printed-node-format=html&output-format=adhoc&compatibility=Enable all extensions&dot-notation=unambiguous&extract-kind=auto">

解释:

  • //div[@class="content"]在question
  • *[not(self::p) and not(self::h3) and not(self::div)]中选择节点,省略子元素: h3,p,div

(或者,如果确实需要过滤div class =“and not(self::div[@class="style"]) and not(self::div[@class="tags"])]”和div class = style).

  • |//div[@class="content"]/text(),则使用空白文本()

连接,而不是任何div标记

实际上,这有点复杂。也许只选择文本或在节点上执行一些DOM操作会更好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51225120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档