XPath to return string concatenation of qualifying child node values

In XPath 1.0:

You can use

/div//text()[not(parent::p)]

to capture the wanted text nodes. The concatenation itself cannot be done in XPath 1.0, I recommend doing it in the host application.


/div//text()

double slash forces to extract text regardless of intermediate nodes


In XPath 2.0 :

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


This look that works:

Using as context /div/:

text() | em/text()

Or without the use of context:

/div/text() | /div/em/text()

If you want to concat the first two strings, use this:

concat(/div/text(), /div/em/text())