Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

python - XPath: select tag with empty value

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>

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.

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 "-------------------------"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How I can find in XPath 1.0 all rows with empty col name="POW"?

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.

This XPath expression:

//row[col[@name = 'POW']
                    [not(*)]
                       [not(normalize-space())]
      ]

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.

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())]
      ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...