XML and namespaces

Alan Kennedy alanmk at hotmail.com
Fri Dec 2 17:35:06 EST 2005


[AMK]
> """
> (I assume not.  Section 1.3.3 of the DOM Level 3 says "Similarly,
> creating a node with a namespace prefix and namespace URI, or changing
> the namespace prefix of a node, does not result in any addition,
> removal, or modification of any special attributes for declaring the
> appropriate XML namespaces."  So the DOM can create XML documents that
> aren't well-formed w.r.t. namespaces, I think.)
> """

[Uche]
> Oh no.  That only means that namespace declaration attributes are not
> created in the DOM data structure.  However, output has to fix up
> namespaces in .namespaceURI properties as well as directly asserted
> "xmlns" attributes.  It would be silly for DOM to produce malformed
> XML+XMLNS, and of course it is not meant to.  The minidom behavior
> needs fixing, badly.

My interpretation of namespace nodes is that the application is 
responsible for creating whatever namespace declaration attribute nodes 
are required, on the DOM tree.

DOM should not have to imply any attributes on output.

#-=-=-=-=-=-=-=-=-=
import xml.dom
import xml.dom.minidom

DAV_NS_U = "http://webdav.org"

xmldoc = xml.dom.minidom.Document()
xmlroot = xmldoc.createElementNS(DAV_NS_U, "DAV:xpg")
xmlroot.setAttributeNS(xml.dom.XMLNS_NAMESPACE, "xmlns:DAV", DAV_NS_U)
xmldoc.appendChild(xmlroot)
print xmldoc.toprettyxml()
#-=-=-=-=-=-=-=-=-=

produces

"""
<?xml version="1.0" ?>
<DAV:xpg xmlns:DAV="http://webdav.org"/>
"""

Which is well formed wrt namespaces.

regards,

-- 
alan kennedy
------------------------------------------------------
email alan:              http://xhaus.com/contact/alan



More information about the Python-list mailing list