[lxml-dev] Strange behaviour with namespaces

Albert Brandl albert.brandl at tttech.com
Mon Jul 23 13:22:49 CEST 2007


Hi!

I'd like to serialize a part of an XML document for later retrieval.
Since the elements are defined using namespaces, I created an 
ElementTree instance for the element to be serialized and called
its write_c14n method.

>>> from import etree
>>> import cStringIO
>>> XML="""
... <n1:a xmlns:a="http://a.org" xmlns:b="http://b.org" xmlns:c="http://c.org">
...   <n2:b>
...     <n3:c/>
...   </n2:b>
... </n1:a>"""
>>> e = etree.fromstring(XML)
>>> et = etree.ElementTree(e[0])
>>> sb = cStringIO.StringIO()
>>> et.write_c14n(sb)
>>> sb.getvalue()
'<b xmlns:a="http://a.org" xmlns:b="http://b.org" xmlns:c="http://c.org">\n    <c></c>\n  </b>'

The xmlns information is transported correctly, but the information about
the namespaces for the elements is lost. I assume this is a bug.

Is there another way to extract a part of the document in textual form
such that namespace information is preserved? Using tostring does not
work, since this method throws away the xmlns attributes altogether.

Thanks & best regards,

Albert Brandl


More information about the lxml-dev mailing list