Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
<td></td><td>foo</td>
我想返回['', 'foo'],但 libxml 的 xpath//td/text()只返回['foo']. 如何找到空标签''而不是(不匹配)?
['', 'foo']
//td/text()
['foo']
''
虽然@Tomalak 完全正确,但在 XPath 2.0 中可以使用:
//td/string(.)
这会产生一系列字符串——每个字符串都包含相应td元素的字符串值。
td
因此,在您的情况下,结果将是所需的结果:
"", "foo"
只要您专门选择文本节点,就不能。因为第一个中根本没有文本节点<td>。
<td>
当您将 XPath 表达式更改为 时'//td',您将获得两个<td>节点。在进一步处理中使用它们的文本值。
'//td'