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