PyXML difficulties

emperorcezar emperorcezar at gmail.com
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?

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

    namespace = 'http://www.w3.org/2003/05/soap-envelope'

    # 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
    doc.appendChild(soapenv)

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

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

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

    header.appendChild(context)

    soapenv.appendChild(header)

    PrettyPrint(doc)

What I'm getting as output:

<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns='urn:zimbra' xmlns:soap='http://www.w3.org/
2003/05/soap-envelope'>
  <soap:Header>
    <context>
    </context>
  </soap:Header>
</soap:Envelope>

What I would expect
<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>
  <soap:Header>
    <context xmlns='urn:zimbra'>
    </context>
  </soap:Header>
</soap:Envelope>



More information about the Python-list mailing list