Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get xmlns attributes using lxml objectify?

I have several xml documents i am dealing with. 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.

>>> 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.

Thanks for the help in advance. Love and peace.

like image 360
Love and peace - Joe Codeswell Avatar asked Jul 04 '26 10:07

Love and peace - Joe Codeswell


1 Answers

The namespace declarations are namespace nodes. Looks like you want the .nsmap property http://lxml.de/tutorial.html#namespaces

xhtml.nsmap
{None: 'http://www.w3.org/1999/xhtml'}
like image 124
nine9ths Avatar answered Jul 06 '26 22:07

nine9ths



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!