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

前端 未结 2 643
慢半拍i
慢半拍i 2020-12-03 22:20

foo

I would like to return [\'\', \'foo\'] but libxml\'s xpath //td/text() returns j

相关标签:
2条回答
  • 2020-12-03 22:37

    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 讨论(0)
  • 2020-12-03 22:46

    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"

    0 讨论(0)
提交回复
热议问题