
Hi there, Jamie Norrish wrote:
It appears as though creating a namespaced element using etree.SubElement(d, 'm:bar', nsmap={'m': ns}) does not work correctly, while using the etree.SubElement(d, '{ns}bar') form does.
That's because that isn't supposed to work -- the ElementTree API works solely with Clarke notation for namespaces, not with namespace prefixes. nsmap is used to generate namespace *declarations* on the Element, so that during serialization the namespace prefixes are generated for that namespace URL.
The attached script hopefully illustrates the problem, and also the way that the serialised XML is incorrect in showing an element which was created with no namespace exactly the same as an element in the default namespace (though it appears the parser picks this up correctly when reparsing the output!?).
Hm, the latter, elements without namespace ending up in the default namespace, does sound like a real problem. I can reproduce it with this line: e = etree.Element('foo', nsmap={None: 'http://default.com'}) where the XML serializes as: <foo xmlns="http://default.com"/> I'm not sure *what* we should expect in this case though, as how would one spell 'foo is in no namespace' in XML? I guess in that case the default namespace could be removed or changed to a namespace prefix that's autogenerated... Anyone have any ideas?
Am I misunderstanding something fundamental?
Yes, but you also pointed out a real problem. Thanks! Regards, Martijn