如何在XPath中为空节点的text()返回“"? [英] How do I return '' for an empty node's text() in XPath?
本文介绍了如何在XPath中为空节点的text()返回“"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<td></td><td>foo</td>
我想返回['', 'foo']
,但是libxml的xpath //td/text()
仅返回['foo']
.如何找到空标签为''
而不是(不匹配)?
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)?
推荐答案
只要您专门选择文本节点,就不能.因为第一个<td>
中根本没有文本节点.
As long as you are selecting text nodes specifically, you can't. Because there simply is no text node in the first <td>
.
将XPath表达式更改为'//td'
时,将获得两个<td>
节点.在进一步处理中使用其文本值.
When you change your XPath expression to '//td'
, you get the two <td>
nodes. Use their text value in further processing.
这篇关于如何在XPath中为空节点的text()返回“"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文