[lxml-dev] redundant namespace declarations

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Mon Dec 4 08:49:22 CET 2006


Hi again,

Stefan Behnel wrote:
> Albert Brandl wrote:
>> The problem occurs with the following code:
>>
>> nsmap = dict (foo="http://foo.org", bar = "http://bar.org")
>> e = Element("{http://foo.org}somefoo", nsmap = nsmap)
>> s = Element("{http://bar.org}somebar", nsmap = nsmap)
>> e.append(s1)
>> et = ElementTree(e)
>> et.write("foo.xml", pretty_print = True)
>>
>> This code creates the following XML file:
>>
>> <foo:somefoo xmlns:foo="http://foo.org" xmlns:bar="http://bar.org">
>>   <bar:somebar xmlns:foo="http://foo.org" xmlns:bar="http://bar.org"/>
>> </foo:somefoo>
>> 
>> Is this a known bug?
> 
> It's known - though not really a bug but rather an inconvenience. Currently,
> we use a function in libxml2 called xmlReconciliateNs() to fix the namespaces
> when merging trees. This function shows the above behaviour. To fix this, we'd
> have to implement our own version, which is a bit tricky and just wasn't
> important enough to try to get right so far. Note that even libxml2 had a
> (minor) bug up to version 2.6.26 here, so it's really not trivial to get this
> kind of thing right.

I finally took a(nother) shot at it and I now have an implementation that can
avoid this kind of problem. It's currently stored in the "nscleanup" branch,
but I will move it to the trunk ASAP. Please give it a try then, to see if it
works nicely for you in other cases where you encountered this.

Stefan



More information about the lxml-dev mailing list