PyXML difficulties

emperorcezar emperorcezar at
Thu May 21 16:58:07 EDT 2009

I'm new to using the xml libs. I'm trying to create xml pragmatically,
but I'm finding an issue. I have two elements I'm creating using
createElementNS two elements (soap:Envelope and context). Each having
a different namespace. When I print the created xml, the namespace
attribute gets moved from the context element to the envelope element.
Is there a reason for this, and how can I get it to not do that?

    from xml.dom import implementation
    from xml.dom.ext import PrettyPrint

    namespace = ''

    # create XML DOM document
    doc = implementation.createDocument(None, '', None)

    # create soap envelope element with namespaces
    soapenv = doc.createElementNS(namespace, "soap:Envelope")

    # add soap envelope element

    # create header element
    header = doc.createElementNS(namespace, "soap:Header")

    context = doc.createElementNS("urn:zimbra", "context")

    context.appendChild(doc.createTextNode(' '))




What I'm getting as output:

<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns='urn:zimbra' xmlns:soap='

What I would expect
<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap=''>
    <context xmlns='urn:zimbra'>

More information about the Python-list mailing list