XPath expression for selecting all text in a given node, and the text of its chldren

xpath

Solution

The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.

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

string(//div[@id='theNode'])

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()

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;

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

Problem

Basically I need to scrape some text that has nested tags. Something like this: ``` <div id='theNode'> This is an <span style="color:red">example</span> <b>bolded</b> text </div> ``` And I want an expression that will produce this: ``` This is an example bolded text ``` I have been struggling with this for hour or more with no result. Any help is appreciated

Original source