Andreas Degert wrote:
In the output the attribute bar should have namespace a, but it has no namespace (the default namespace doesn't apply to attributes as specified in http://www.w3.org/TR/REC-xml-names/#scoping-defaulting, section 6.2).
Element("top", {"bar":"", "{a}bar":""}, nsmap={None:"a","b":"b"})
yields <top xmlns="a" xmlns:b="b" bar="" bar=""/>
The default namespace is a special case because it doesn't apply to attributes (this means when attributes have a namespace value they must be serialized with a prefix).
I see the problem. Actually, now that you mention it, it is not uncommon to define multiple prefixes for a namespace, e.g. in XSD or WSDL. Maybe we can somehow prioritise namespace declarations on the way in, or special case the default namespace in the cleanup procedure (like: making sure it comes last in the declaration list, although that wouldn't impact the parser). It would be nice to have some simple rules how to check that this has to be done, as it definitely adds overhead. I could even accept not simplifying the nsmap at all, but there still is the problem of namespace cleanup when moving elements (moveNodeToDocument() in proxi.pxi). We would need special rules there, too, like: allow adding a second prefix for the default namespace - no idea if that case is easy to recognise and handle.
When serializing elements the default namespace should have a higher priority, i.e. those elements can be written without prefix.
The serialiser is part of libxml2. If you want changes in this part of lxml, ask on the libxml2 mailing list. However, I think the more general problem is in lxml here. Stefan