pythonxmlelementtreecelementtree

Python ElementTree namespace registering with two "empty prefix" namespaces


I am parsing an XML document containing 3 different namespaces, where 2 of them do not have a prefix. This XML-doc looks like:

<element xmlns="namespace1">
   <child></child>
   <child xmlns="namespace2" xlmns:a="namespace3>
      <a:childs></childs> 
</element>   

So basically, namespace1 is used for the first two rows, namespace2 for the third row and namespace3 for the fourth row. However, when I register the namespaces like

ET.register_namespace('', "namespace1")
ET.register_namespace('', "namespace2")
ET.register_namespace('a', "namespace3")

my output document eventually puts "ns1:" in front of everything regarding namespace2. If I change the prefix of one of those to for example 'b' it obviously puts this 'b' in front of the regarding element names, which I am trying to avoid. Does anyone know how to solve this? Thanks in advance.


Solution

  • I would recommend to use lxml library, because it's more flexible when working with namespaces.

    Here is an example of constructing XML with the desired structure:

    from lxml import etree
    
    nsmap1 = { None: "namespace1"}
    nsmap2 = {None: "namespace2", 'a': "namespace3"}
    
    root = etree.Element('element', nsmap=nsmap1)
    child1 = etree.Element('child', nsmap=nsmap1)
    child2 = etree.Element('child', nsmap=nsmap2)
    child2.insert(1, etree.Element('{namespace3}childs', nsmap=nsmap2))
    root.insert(1, child1)
    root.insert(2, child2)
    
    print(etree.tostring(root))
    

    Output:

    <element xmlns="namespace1">
        <child/>
        <child xmlns="namespace2" xmlns:a="namespace3">
            <a:childs/>
        </child>
    </element>
    

    You have default namespaces (namespace without prefix) declared 2 times in XML document, therefore it would be convenient to define 2 namespace dictionaries (nsmap1 and nsmap2) containing their own mapping of default namespace.