How to customize namespace prefix in objects inheriting from lxml.ElementBase

Dear fellow developers, I originally posted my questions on stackoverflow: http://stackoverflow.com/questions/9265534/how-to-customize-namespace-prefix... objects-inheriting-from-lxml-elementbase I understand that custom XML elements should inherit from `ElementBase`. For instance, I can create class FactVariable(etree.ElementBase): ''' Class that represents a XBRL fact variable.''' TAG = '{http://xbrl.org/2008/variable}factVariable' @property def label(self): return self.attrib['{http://www.w3.org/1999/xlink}label'] @label.setter def label(self, value): self.attrib['{http://www.w3.org/1999/xlink}label'] = value My problem is that when I create a XML tree and place such nodes, I get <ns0:factVariable xmlns:ns0="http://xbrl.org/2008/variable" label="azerty"/> **Question**: I want the namespace to be prefixed `va`, not `ns0` How can I change that? I tried to set the `self.nsmap` property, but I have a "read-only" exception. Adding a key/value has no effect (as said in the documentation). I also tried, without success etree.register_namespace('va', 'http://xbrl.org/2008/variable') Thanks in advance Régis

RégisDécamps, 21.02.2012 17:11:
What is your use case for that? I consider prefixes a serialisation detail that is best ignored.
How can I change that?
You can't currently set that on a per-element basis for custom element classes. It also wouldn't change much, because the prefixes are subject to tree normalisation operations, which means that they may still end up inheriting a prefix from a parent at some point. What you can do is to set the prefix on the root element, so that children can inherit it.
I also tried, without success
etree.register_namespace('va', 'http://xbrl.org/2008/variable')
Hmm, that should generally work, though. I wonder why it wouldn't... Stefan

RégisDécamps, 21.02.2012 17:11:
What is your use case for that? I consider prefixes a serialisation detail that is best ignored.
How can I change that?
You can't currently set that on a per-element basis for custom element classes. It also wouldn't change much, because the prefixes are subject to tree normalisation operations, which means that they may still end up inheriting a prefix from a parent at some point. What you can do is to set the prefix on the root element, so that children can inherit it.
I also tried, without success
etree.register_namespace('va', 'http://xbrl.org/2008/variable')
Hmm, that should generally work, though. I wonder why it wouldn't... Stefan
participants (2)
-
Régis Décamps
-
Stefan Behnel