Python, XML, Namespaces

Joined
Sep 9, 2006
Messages
1
Reaction score
0
Howdy, I'm looking to generate an XML document using Python and minidom with namespaces in it. For some reason every time I create a new namespace it adds that namespace to the root element, and I'd rather it show up in the element that I created it with.

For example:

encoding = "http://schemas.xmlsoap.org/soap/encoding/"
envelopeNS = "http://schemas.xmlsoap.org/soap/envelope/"
upnpNS = "urn:schemas-upnp-org:service:ContentDirectory:1"

docType = implementation.createDocumentType('','','')
response = implementation.createDocument('',None,docType)

envelope = response.createElementNS(envelopeNS, "s:Envelope")

envelope.setAttributeNS(envelopeNS, "s:encodingStyle", encoding)

body = response.createElementNS(envelopeNS, "s:Body")
browseResponse = response.createElementNS(upnpNS, "u:BrowseResponse")

result = response.createElement("Response")


After building and outputting with PrettyPrint I get

<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' xmlns:u='urn:schemas-upnp-org:service:ContentDirectory:1' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<s:Body>
<u:BrowseResponse>
<Response>ERROR</Response>

Notice the namespace "u" and "s" are both in the s:Envelope element- what I'd like is

<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<s:Body>
<u:BrowseResponse xmlns:u='urn:schemas-upnp-org:service:ContentDirectory:1'>
<Response>ERROR</Response>

No idea why minidom wants to collect all the namespaces together like that (note that BrowseResponse was created with createElementNS, etc.)

Thanks for any tips,
Joe
 

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,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top