html - XPath 返回由 html 标签分割的字符串连接

标签 html xml xpath string-concatenation

如何使用 XPath 表达式返回包含连接值的字符串值?

<div>
This text node (1) should be returned.
<em>And the value of this element.</em>
And this.
</div>

<div>
This text node (2) should be returned.
And this.
</div>

<div>
This text node (3) should be returned.
<em>And the value of this element.</em>
And this.
</div>

返回的值应该是由 div 元素分割的字符串数组:

"This text node (1) should be returned. And the value of this element. And this."
"This text node (2) should be returned. And this."
"This text node (3) should be returned. And the value of this element. And this."

这可以在单个 XPath 表达式中实现吗?

最佳答案

XPath 1.0

无法使用纯 XPath 1.0。相反,选择 div 元素:

//div

然后对托管 XPath 库调用的语言中的每个 div 元素的字符串值应用空间规范化。

XPath 2.0

这个 XPath 2.0 表达式,

//div/normalize-space()

将返回文档中所有div元素的规范化字符串值:

This text node (1) should be returned. And the value of this element. And this.
This text node (2) should be returned. And this.
This text node (3) should be returned. And the value of this element. And this.

按要求。

关于html - XPath 返回由 html 标签分割的字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46995268/

相关文章:

python - 在 python 中使用 lxml 解析标签中的 XML 数据

java - XPath 转义字符 (HtmlUnit)

php - 向网页添加语法高亮 (HTML/php)

html - 如何让我的页脚随内容推送?

html - 加位置:relative to every layout element?有什么好处

javascript - 使用 webkitbrowser 获取通过注入(inject)的 javascript 修改的输入文本的值时出错

selenium - Selenium 中::的含义是什么

css - div 表格行为

c++ - 无法在给定的 xml 文件 libxml2 中正确添加子节点

Java XML 在特定节点上拉取数据