Reputation: 4421
I use lxml
to parse well-formatted xml:
<search-results xmlns="http://www.w3.org/2005/Atom"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:prism="http://prismstandard.org/namespaces/basic/2.0/"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<opensearch:totalResults>0</opensearch:totalResults>
<opensearch:startIndex>0</opensearch:startIndex>
<opensearch:itemsPerPage>0</opensearch:itemsPerPage>
<entry>
<error>Result set was empty</error>
</entry>
</search-results>
I am interested in the text inside error
.
I was using the following code:
from lxml import etree
doc = etree.fromstring(xml) # xml is above xml
ns = {'opensearch': "http://a9.com/-/spec/opensearch/1.1/"}
print doc.xpath('//opensearch:totalResults', namespaces=ns)[0].text
which works fine to get a 0
, but what should I do for <entry>
, which doesn't seem to be in a namespace? I tried adding the empty namespace, which I think is associated with "http://www.w3.org/2005/Atom"
:
ns = {'opensearch': "http://a9.com/-/spec/opensearch/1.1/", 'empty': "http://www.w3.org/2005/Atom"}
print doc.xpath('//entry/error', namespaces=ns)[0].text
But this results in an IndexError
, because there is no list.
Upvotes: 1
Views: 118
Reputation: 473763
You need to use that empty
alias you gave to the empty namespace inside the expression:
ns = {'opensearch': "http://a9.com/-/spec/opensearch/1.1/", 'empty': "http://www.w3.org/2005/Atom"}
print doc.xpath('//empty:entry/empty:error', namespaces=ns)[0].text
Upvotes: 1