xpathlibxml2

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


<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)?


Solution

  • As long as you are selecting text nodes specifically, you can't. Because there simply is no text node in the first <td>.

    When you change your XPath expression to '//td', you get the two <td> nodes. Use their text value in further processing.