开发者

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

开发者 https://www.devze.com 2022-12-23 00:43 出处:网络
<td><开发者_StackOverflow;/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 \'\' in

<td><开发者_StackOverflow;/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)?


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"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号