如何使用 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/