XPath to return string concatenation of qualifying child node values

Tim Coulter picture Tim Coulter · Sep 10, 2009 · Viewed 49.3k times · Source

Can anyone please suggest an XPath expression format that returns a string value containing the concatenated values of certain qualifying child nodes of an element, but ignoring others:

<div>
    This text node should be returned.
    <em>And the value of this element.</em>
    And this.
    <p>But this paragraph element should be ignored.</p>
</div>

The returned value should be a single string:

This text node should be returned. And the value of this element. And this.

Is this possible in a single XPath expression?

Thanks.

Answer

Dimitre Novatchev picture Dimitre Novatchev · Sep 10, 2009

In XPath 2.0 :

string-join(/*/node()[not(self::p)], '')