[lxml-dev] prefix mappings

FnH FnH at antwerpen.be
Sat Sep 29 22:03:14 CEST 2007


Hi,

I would like to generate the following serialization:

<a xmls="foo">
<b xmls="bar"/>
</a>

Currently, the closest I can come is:

<a xmls="foo" xmlns:x="bar">
<x:b/>
</a>

using the following code:

a = Element("{foo}a", nsmap={None:"foo", "x":"bar"})
a.append(Element("{bar}b"))

Note that even though these serializations are equivalent, sometimes 
remapping namespaces is neccessary to work around faulty implementations 
in other products.
The feed validator for example warns against using namespace prefixes on 
elements for good reason. If you don't, many readers aren't able to 
parse your feed 
(http://www.intertwingly.net/wiki/pie/XmlNamespaceConformanceTests). If 
you're using xhtml as content this means remapping the default namespace.

In order to solve this I think it would be a good idea to allow (or take 
into account) prefix mappings on non root nodes as well. The output I'd 
like could then be achieved by the following code snippet:

a = Element("{foo}a", nsmap={None:"foo"})
a.append(Element("{bar}b", nsmap={None:"bar"}))

What do you think?

Regards,

Nick


More information about the lxml-dev mailing list