Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath获取忽略子节点的主段落文本_Xpath - Fatal编程技术网

Xpath获取忽略子节点的主段落文本

Xpath获取忽略子节点的主段落文本,xpath,Xpath,我想匹配以下代码的主要段落内容,省略子节点p,div,h3 <div class="content"> sunday, monday, tuesday, <br> <br> wednesday, thursday, <br> friday, saturday <div class ="tags">sunday</div> <h3>Days</h

我想匹配以下代码的主要段落内容,省略子节点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=“tags”,h3,p,div class=style.

这应该可以做到:

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

说明:

  • //div[@class=“content”]
    选择相关节点
  • *[not(self::p)和not(self::h3)以及not(self::div)]
    省略子元素:h3,p,div
    (或者代替任何div
    和not(self::div[@class=“style”])和not(self::div[@class=“tags”])
    ,如果您确实需要过滤div class=“tags”和div class=style)
  • >代码/> /div [ @类=“内容”] /文本()/代码>然后,加入空白文本()/LI>

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

/div[@class=“content”]/text()
应该返回您所需的输出。它将只返回第一个文本,即“星期日、星期一、星期二”。否。它应该返回文本节点列表。共享您尝试过的代码,并为您使用的工具添加标记这是我第一次尝试的,它只返回第一个文本。使用
//div[@class=“content”]/text()
时,它会自动转换为
//div[@class=“content”]/text()[1]
//div[@class="content"]/*[not(self::p) and not(self::h3) and not(self::div)]|//div[@class="content"]/text()