Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return '' for an empty node's text() in XPath?

Tags:

xpath

libxml2

<td></td><td>foo</td>

I would like to return ['', 'foo'] but libxml's xpath //td/text() returns just ['foo']. How do I find the empty tag as '' instead of (not matched)?

like image 992
joeforker Avatar asked Mar 10 '10 21:03

joeforker


1 Answers

While @Tomalak is perfectly right, in XPath 2.0 one can use:

//td/string(.)

and this produces a sequence of strings -- each one containing the string value of a corresponding td element.

So, in your case the result will be the desired one:

"", "foo"

like image 178
Dimitre Novatchev Avatar answered Sep 22 '22 13:09

Dimitre Novatchev