PyXML difficulties

E

emperorcezar

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>
 
P

Paul Boddie

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?

Having the default namespace declared on an ancestor of the context
element is completely valid in this case, since the soap:Envelope
element is associated with a different namespace. You could see what
happens if you serialise the document using a different library
function or document/node method, however.
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)

[...]

Note that according to the specifications, the element name should be
None (null) for this kind of createDocument invocation. See here for
details:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#Level-2-Core-DOM-createDocument

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top