问题
XPath returns nothing if a child element has no text value. In this case, rating has no data, so I want it to say so - None or nothing in this child instead of just ignoring it. Your input is much appreciated.
XML :
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
<rating></rating>
</book>
<book>
<title lang="hindi">Learning XML</title>
<price>39.95</price>
<rating></rating>
</book>
</bookstore>
Python :
>>> import lxml.html as lh
>>> bk=open('book.xml','r')
>>> bkout=lh.parse(bk)
>>> bk.close()
>>> bkout.xpath('//book/*/text()')
['Harry Potter', '29.99', 'Learning XML', '39.95']
>>> bkout.xpath('//book/* and not(text())/text()')
True
Desired Output :
['Harry Potter', '29.99', '', 'Learning XML', '39.95', '']
or
['Harry Potter', '29.99', None, 'Learning XML', '39.95', None]
回答1:
Remove the "text()":
In [16]: [x.text for x in bk.xpath("//book/*")]
Out[16]: ['Harry Potter', '29.99', None, 'Learning XML', '39.95', None]
来源:https://stackoverflow.com/questions/11105900/how-to-make-xpath-return-none-in-python-if-no-data-found