XML and namespaces

uche.ogbuji at gmail.com uche.ogbuji at gmail.com
Fri Dec 2 09:16:29 EST 2005


Quoting Andrew Kuchling:
"""
>         >>> element = document.createElementNS("DAV:", "href")

This call is incorrect; the signature is createElementNS(namespaceURI,
qualifiedName).
"""

Not at all, Andrew.  "href" is a valid qname, as is "foo:href".  The
prefix is optional in a QName.  Here is the correct behavior, taken
from a non-broken DOM library (4Suite's Domlette)

>>> from Ft.Xml import Domlette
>>> document = Domlette.implementation.createDocument(None, None, None)
>>> element = document.createElementNS("DAV:", "href")
>>> document.appendChild(element)
<Element at 0xb7d12e2c: name u'href', 0 attributes, 0 children>
>>> Domlette.Print(document)
<?xml version="1.0" encoding="UTF-8"?>
<href xmlns="DAV:"/>>>>

"""
If you call .createElementNS('whatever', 'DAV:href'),
the output is the expected:
        <?xml version="1.0" ?><DAV:href/>
"""

Oh, no.  That is not at all expected.  The output should be:

        <?xml version="1.0" ?><DAV:href xmlns:DAV="whatever"/>

"""
It doesn't look like there's any code in minidom that will
automatically create an 'xmlns:DAV="whatever"' attribute for you.  Is
this automatic creation an expected behaviour?
"""

Of course.  Minidom implements level 2 (thus the "NS" at the end of the
method name), which means that its APIs should all be namespace aware.
The bug is that writexml() and thus toxml() are not so.

"""
(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.)
"""

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.

--
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/




More information about the Python-list mailing list