E-factory - ElementMaker - RSS and namespaces
![](https://secure.gravatar.com/avatar/e0b32392adcf3ae620ecebdedd3114a3.jpg?s=120&d=mm&r=g)
Hi, Short version of my question: The tutorial (http://lxml.de/tutorial.html#the-e-factory) shows how to create a basic xml document without a namespace and then an example with a namespace. But how do you apply the namespace to only on element? e.g. in the RSS sample below. More details: I recently found the lxml library and have found it very useful. So far I have been able to generate a standard RSS file. I used python like this: from lxml.builder import E from lxml import etree rss = ( E.rss( E.channel( E.title("Page Title"), E.link(""), E.description("Feed info"), ) ) ) tree = etree.ElementTree(rss) channel = rss[0] item = (E.item( E.title("Item-%03i" % (i)), E.link("http://example.com/%i" % (i)), E.description("this is a"), E.pubdate('Mon, %02i Jun 2011 11:00:00 +0100' % (day_num)), )) channel.append(item) I have just cut out a smaller part of a working script here. Eventually I will use other logic to generate the items. This example was just in a simple loop to create 3 items. Now that's all been going fine, but I wanted to open an existing RSS file and sort the items, I found that is not going to work with the pubdate as its in the RFC 822 format. So I started looking at the dublin core namespace. But I have not found a way to use the above method and add only the 'dc:' prefix to certain elements. The lxml tutorial (http://lxml.de/tutorial.html#the-e-factory) shows how to create a basic xml document without a namespace and then an example with a namespace. But how do you apply the namespace to only on element? e.g. in the RSS below. Mainly to add the date that is sortable. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> <title>Example feed</title> <link>http://example.feed.com</link> <description>Example with the dc field</description> <item> <title>Example item</title> <link>http://example.feed.com/1</link> <guid>http://example.feed.com/1</guid> <pubDate>Thu, 25 Feb 2010 21:56:24 GMT</pubDate> <dc:source>http://feed.com/</dc:source> <dc:date>2010-02-25T21:56:24+00:00</dc:date> </item> </channel> </rss> Thanks in advance. Steve
![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
dev@kihonkai.com, 24.06.2011 19:39:
The tutorial (http://lxml.de/tutorial.html#the-e-factory) shows how to create a basic xml document without a namespace and then an example with a namespace.
But how do you apply the namespace to only on element?
It's usually preferable to predefine the tag names, as presented in the second example in the tutorial. Then, you can use getattr() to apply namespaces to single elements, if they differ from the 'normal' namespace of the other tags. Or just use a separate ElementMaker instance. Stefan
![](https://secure.gravatar.com/avatar/e0b32392adcf3ae620ecebdedd3114a3.jpg?s=120&d=mm&r=g)
Stefan Behnel <stefan_ml <at> behnel.de> writes:
dev <at> kihonkai.com, 24.06.2011 19:39:
The tutorial (http://lxml.de/tutorial.html#the-e-factory) shows how to create a basic xml document without a namespace and then an example with a namespace.
But how do you apply the namespace to only on element?
It's usually preferable to predefine the tag names, as presented in the second example in the tutorial. Then, you can use getattr() to apply namespaces to single elements, if they differ from the 'normal' namespace of the other tags. Or just use a separate ElementMaker instance.
Stefan
That has cleared it up now and I was able to get it working using a second ElementMaker instance. I couldn't find anything about getattr() exist on the lxml.objectify page. I've not looked at this at all yet. But using two ElementMaker instances will work perfectly fine. I was quite close but was just short of getting it working. Oddly this was the first time I finally got round to trying a mailing list. I'm glad I did as it's very easily and quickly solved my issue. Many Thanks, Steve
![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
Steve, 28.06.2011 22:44:
Stefan Behnel writes:
dev<at> kihonkai.com, 24.06.2011 19:39:
The tutorial (http://lxml.de/tutorial.html#the-e-factory) shows how to create a basic xml document without a namespace and then an example with a namespace.
But how do you apply the namespace to only on element?
It's usually preferable to predefine the tag names, as presented in the second example in the tutorial. Then, you can use getattr() to apply namespaces to single elements, if they differ from the 'normal' namespace of the other tags. Or just use a separate ElementMaker instance.
That has cleared it up now and I was able to get it working using a second ElementMaker instance. I couldn't find anything about getattr() exist on the lxml.objectify page. I've not looked at this at all yet.
That's ok, one ElementMaker per namespace is the intended usage pattern.
But using two ElementMaker instances will work perfectly fine. I was quite close but was just short of getting it working.
I'll add a note to the tutorial.
Oddly this was the first time I finally got round to trying a mailing list. I'm glad I did as it's very easily and quickly solved my issue.
Happy to be of help. Stefan
participants (3)
-
dev@kihonkai.com
-
Stefan Behnel
-
Steve