[issue13378] Change the variable "nsmap" from global to instance (xml.etree.ElementTree)
report at bugs.python.org
Fri Nov 11 08:06:18 CET 2011
Stefan Behnel <scoder at users.sourceforge.net> added the comment:
Florent, thanks for the notification.
Nekmo, note that you are misusing this feature. The _namespace_map is meant to provide "well known namespace prefixes" only, so that common namespaces end up using the "expected" prefix. This is also the reason why it maps namespaces to prefixes and not the other way round. It is not meant to temporarily assign arbitrary prefix to namespaces. That is the reason for it being a global option.
That being said, lxml.etree's Element factory takes an "nsmap" parameter that implements the feature you want. It's documented here:
Note that it maps prefixes to namespaces and not the other way round. This is because there is a corresponding "nsmap" property on Elements that provides the currently defined prefixes in the context of an Element. ElementTree itself does not (and cannot) support this property because it drops the prefixes during parsing. However, I would still request that an implementation of the parameter to the Element() factory should be compatible for both libraries.
Also look for "nsmap" in the compatibility docs (appears in two sections):
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list