XPath:选择值为空的标签 [英] XPath: select tag with empty value
问题描述
如何在XPath 1.0中查找所有col name="POW"
为空的行?
How I can find in XPath 1.0 all rows with empty col name="POW"
?
<row>
<col name="WOJ">02</col>
<col name="POW"/>
<col name="GMI"/>
<col name="RODZ"/>
<col name="NAZWA">DOLNOŚLĄSKIE</col>
<col name="NAZDOD">województwo</col>
<col name="STAN_NA">2011-01-01</col>
</row>
我尝试了许多解决方案.在Firefox扩展中选择XPath Checker的次数很少,但是lxml.xpath()
表示表达式无效或仅不返回任何行.
I tried many solutions. Few times in Firefox extension XPath Checker selection was ok, but lxml.xpath()
says that expression is invalid or just returns no rows.
我的Python代码:
My Python code:
from lxml import html
f = open('TERC.xml', 'r')
page = html.fromstring(f.read())
for r in page.xpath("//row[col[@name = 'POW' and not(text())]]"):
print r.text_content()
print "-------------------------"
推荐答案
如何在XPath 1.0中查找所有
col name="POW"
为空的行?
空"有许多可能的定义,并且每个定义都有一个不同的XPath表达式来选择空"元素.
There are many possible definitions of "empty" and for each one of them there is a different XPath expression selecting "empty" elements.
对于空元素的合理定义是:没有子元素且没有文本节点子元素的元素,或者具有单个文本节点子元素的元素,其字符串值仅包含空格字符.
A reasonable definition for an empty element is: an element that has no children elements and no text-node children, or an element that has a single text-node child, whose string value contains only whitespace characters.
此XPath表达式:
//row[col[@name = 'POW']
[not(*)]
[not(normalize-space())]
]
选择XML文档中的所有row
元素,这些元素具有col
子元素,该元素具有属性name
的字符串值"POW"
并且没有子元素-元素并且其字符串值完全由以下元素组成:空格字符,或者是空字符串.
selects all row
elements in the XML document, that have a col
child, that has an attribute name
with string value "POW"
and that has no children - elements and whose string value consists either entirely of whitespace characters, or is the empty string.
如果空"表示您理解为根本没有子代" ,这意味着没有子代元素,没有子代PI节点,也没有子代注释节点,请使用:
In case by "empty" you understand "having no children at all", which means no children elements and no children PI nodes and no children comment nodes, then use:
//row[col[@name = 'POW']
[not(node())]
]
这篇关于XPath:选择值为空的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!