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

时间:2017-10-28 22:51:33

标签: 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表达式中使用?

1 个答案:

答案 0 :(得分:2)

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.

按要求。