简体   繁体   English

如何使用lxml objectify获取xmlns属性?

[英]How to get xmlns attributes using lxml objectify?

I have several xml documents i am dealing with. 我有几个xml文件正在处理。 They have differing root elements. 他们有不同的根元素。 Here are some of them. 这里是其中的一些。

<rss xmlns:npr="http://www.npr.org/rss/" xmlns:nprml="http://api.npr.org/nprml" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.thisamericanlife.org/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0" xml:base="http://www.thisamericanlife.org">

I am using lxml in the following way on the first example from above. 我在上面的第一个例子中以下列方式使用lxml。

>>> from lxml import objectify
>>> root = objectify.parse('file_for_first_example').getroot() # contains valid xml with first above element
>>> print root.tag
'rss'
>>> root.attrib.keys()
['version']
>>> for k in root.attrib.iterkeys():
>>>    print k
version
>>> print root.get("xmlns:npr")
None

I just want to be able to sense what these 'attribute' values are so i can, i believe, infer what the format of the various feeds are. 我只是想能够感知这些'属性'值是什么,所以我可以,我相信,推断出各种Feed的格式是什么。

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。 Love and peace. 爱与和平。

The namespace declarations are namespace nodes. 名称空间声明是名称空间节点。 Looks like you want the .nsmap property http://lxml.de/tutorial.html#namespaces 看起来你想要.nsmap属性http://lxml.de/tutorial.html#namespaces

xhtml.nsmap
{None: 'http://www.w3.org/1999/xhtml'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM