用于选择给定节点中所有文本的XPath表达式及其chldren的文本

Mar*_*ski 12 xpath

基本上我需要刮一些嵌套标签的文本.

像这样的东西:

<div id='theNode'>
This is an <span style="color:red">example</span> <b>bolded</b> text
</div>
Run Code Online (Sandbox Code Playgroud)

我想要一个表达式来产生这个:

This is an example bolded text
Run Code Online (Sandbox Code Playgroud)

我一直在努力工作一小时或更长时间没有结果.

任何帮助表示赞赏

Lac*_*che 22

所述字串值元素节点的是在文档顺序元素节点的所有文本节点的后代的字符串值的串接.

You want to call the XPath string() function on the div element.

string(//div[@id='theNode'])
Run Code Online (Sandbox Code Playgroud)

You can also use the normalize-space function to reduce unwanted whitespace that might appear due to newlines and indenting in the source document. This will remove leading and trailing whitespace and replace sequences of whitespace characters with a single space. When you pass a nodeset to normalize-space(), the nodeset will first be converted to it's string-value. If no arguments are passed to normalize-space it will use the context node.

normalize-space(//div[@id='theNode'])

// if theNode was the context node, you could use this instead
normalize-space()
Run Code Online (Sandbox Code Playgroud)

You might want use a more efficient way of selecting the context node than the example XPath I have been using. eg, the following Javascript example can be run against this page in some browsers.

var el = document.getElementById('question');
var result = document.evaluate('normalize-space()', el, null ).stringValue;
Run Code Online (Sandbox Code Playgroud)

The whitespace only text node between the span and b elements might be a problem.