Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to return string concatenation of qualifying child node values

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.

like image 978
Tim Coulter Avatar asked Sep 10 '09 08:09

Tim Coulter


People also ask

What is child :: In XPath?

As defined in the W3 XPath 1.0 Spec, " child::node() selects all the children of the context node, whatever their node type." This means that any element, text-node, comment-node and processing-instruction node children are selected by this node-test.

How do I select all child elements in XPath?

For the div element with an id attribute of hero //div[@id='hero'] , these XPath expression will select elements as follows: //div[@id='hero']/* will select all of its children elements. //div[@id='hero']/img will select all of its children img elements. //div[@id='hero']//* will select all of its descendent elements.

How do I select the first child in XPath?

The key part of this XPath is *[1] , which will select the node value of the first child of Department .


2 Answers

In XPath 2.0 :

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

like image 200
Dimitre Novatchev Avatar answered Oct 07 '22 07:10

Dimitre Novatchev


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.

like image 20
Tomalak Avatar answered Oct 07 '22 08:10

Tomalak