[lxml-dev] pretty-printing

Albert Brandl albert.brandl at tttech.com
Mon Nov 20 15:41:36 CET 2006


Hi!

I've posted a question about pretty-printing some time ago. Now I 
finally found the time to construct an example.

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>

The problem is that "bar:somebar" redundantly declares the namespaces
for "foo" and "bar", which affects both the readability and the size
of the XML file.

If the element "s" is appended to "e" by using the SubElement function
instead of "append", the content of the XML file looks like I'd expect:

<foo:somefoo xmlns:foo="http://foo.org" xmlns:bar="http://bar.org">
  <bar:somebar/>
</foo:somefoo>

Is this a known bug?

Thanks & best regards,

Albert Brandl


More information about the lxml-dev mailing list