3

From the lxml documentation, 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')

1 Answer 1

3

I know this is old and probably @rds solved this, but I was encountering a similar problem, so thought I'd share som info. It seems that setting the nsmap upon creation will at least work:

>>> etree.tostring(FactVariable(label='qwerty', nsmap={'va':'http://xbrl.org/2008/variable'}))
'<va:factVariable xmlns:va="http://xbrl.org/2008/variable" label="qwerty"/>'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.