如何在lxml xpath查询中使用空名称空间? [英] how do I use empty namespaces in an lxml xpath query?
本文介绍了如何在lxml xpath查询中使用空名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下格式的xml文档:
I have an xml document in the following format:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gsa="http://schemas.google.com/gsa/2007">
...
<entry>
<id>https://ip.ad.dr.ess:8000/feeds/diagnostics/smb://ip.ad.dr.ess/path/to/file</id>
<updated>2011-11-07T21:32:39.795Z</updated>
<app:edited xmlns:app="http://purl.org/atom/app#">2011-11-07T21:32:39.795Z</app:edited>
<link rel="self" type="application/atom+xml" href="https://ip.ad.dr.ess:8000/feeds/diagnostics"/>
<link rel="edit" type="application/atom+xml" href="https://ip.ad.dr.ess:8000/feeds/diagnostics"/>
<gsa:content name="entryID">smb://ip.ad.dr.ess/path/to/directory</gsa:content>
<gsa:content name="numCrawledURLs">7</gsa:content>
<gsa:content name="numExcludedURLs">0</gsa:content>
<gsa:content name="type">DirectoryContentData</gsa:content>
<gsa:content name="numRetrievalErrors">0</gsa:content>
</entry>
<entry>
...
</entry>
...
</feed>
我需要使用lxml中的xpath检索所有entry
元素.我的问题是我不知道如何使用空的名称空间.我尝试了以下示例,但是没有用.请告知.
I need to retrieve all entry
elements using xpath in lxml. My problem is that I can't figure out how to use an empty namespace. I have tried the following examples, but none work. Please advise.
import lxml.etree as et
tree=et.fromstring(xml)
我尝试过的各种方法是:
The various things I have tried are:
for node in tree.xpath('//entry'):
或
namespaces = {None:"http://www.w3.org/2005/Atom" ,"openSearch":"http://a9.com/-/spec/opensearchrss/1.0/" ,"gsa":"http://schemas.google.com/gsa/2007"}
for node in tree.xpath('//entry', namespaces=ns):
或
for node in tree.xpath('//\"{http://www.w3.org/2005/Atom}entry\"'):
在这一点上,我只是不知道该尝试什么.任何帮助,我们将不胜感激.
At this point I just don't know what to try. Any help is greatly appreciated.
推荐答案
类似的方法应该起作用:
Something like this should work:
import lxml.etree as et
ns = {"atom": "http://www.w3.org/2005/Atom"}
tree = et.fromstring(xml)
for node in tree.xpath('//atom:entry', namespaces=ns):
print node
另请参见 http://lxml.de/xpathxslt.html#namespaces-and-prefixes
替代:
for node in tree.xpath("//*[local-name() = 'entry']"):
print node
这篇关于如何在lxml xpath查询中使用空名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文