
Hi, I made an XML doc grabbing and assembling nodes from various stuffs. namespace declarations are consistent but repeated among various descendant elements. And I want to gather all these namespace declarations in the root element. i. e. when I have : <root> <foo xmlns:abc="abc.namespace"> <abc:bar attrib="value"> blah </abc:bar> </foo> <joe xmlns:abc="abc.namespace"> <abc:stuff> stuff </abc:stuff> </joe> </root> Then I need (once serialized with .tostring(...) : <root xmlns:abc="abc.namespace"> <foo> <abc:bar attrib="value"> blah </abc:bar> </foo> <joe> <abc:stuff> stuff </abc:stuff> </joe> </root> I naively thought that merging all elements .nsmap attribs into the root.nsmap should do the job, but it appears that changes to the Element.nsmap mapping fail silently. My code (what I want to do with a really mutable .nsmap) : ---- etree.cleanup_namespaces(doc) for elem in doc.getiterator(): if hasattr(elem, 'nsmap'): for k in elem.nsmap: doc.nsmap[k] = elem.nsmap.pop(k) ---- Unfortunately .nsmap attrib is a so called "non mutable dict but ignoring silently mutation attempts". Thus this code does not work as expected. Any hint to unlock me is welcome. Best regards -- Gilles Lenfant