How to get all descendant text content in XML using XPath

XML file

<TEXT>
    <DESCR>
         Here is the first part...
         <PLUS>The second</PLUS>
         And the third
    </DESCR>
</TEXT>

What I expect to get:

Here is the first part...The secondAnd the third

What I actually get:

Here is the first part...And the third.

I tried descendant-or-self::* xPath function, child, and descendant, no result.

If someone can tell me how to get the text in the child nodes too.

Answers


XPath 1.0

You cannot perform the concatenation of all text descendents of a given node within XPath 1.0. You can select the nodes in XPath,

/TEXT/DESCR//text()

but then you'll have to perform the concatenation in the hosting language.

In PHP:

$xml = '<TEXT>
    <DESCR>
         Here is the first part...
         <PLUS>The second</PLUS>
         And the third
    </DESCR>
</TEXT>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$x= new DOMXpath($dom);
foreach($x->query("/TEXT/DESCR//text()") as $node) echo trim($node->textContent); 

Will output the result you requested:

Here is the first part...The secondAnd the third

[Alternatively if you've no other reason to iterate over the text nodes, replace the foreach loop above with:]

$xml = '<TEXT>
    <DESCR>
         Here is the first part...
         <PLUS>The second</PLUS>
         And the third
    </DESCR>
</TEXT>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$x= new DOMXpath($dom);
echo str_replace(PHP_EOL, '', $x->evaluate('normalize-space(/TEXT/DESCR)'));

Which yields:

Here is the first part... The second And the third
XPath 2.0

You can perform the concatenation of all text descendents of a given node within XPath 2.0:

string-join(/TEXT/DESCR//text(), '')

Need Your Help

What good is the monad instance of Cont?

haskell monads continuation-passing

I'm playing around with CPS and Control.Monad.Cont and wonder what we gain by noticing the monadic structure. For code like this:

SQL CE Insert error

c# sql mobile

I'm creating an application for PDA (WM 6).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.