XML and namespaces

Paul Boddie paul at boddie.org.uk
Sun Dec 11 12:25:06 EST 2005


Alan Kennedy wrote:

[Discussing the appearance of xmlns="DAV:"]

> But that's incorrect. You have now defaulted the namespace to "DAV:" for
> every unprefixed element that is a descendant of the href element.

[Code creating the no_ns element with namespaceURI set to None]

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

I must admit that I was focusing on the first issue rather than this
one, even though it is related, when I responded before. Moreover,
libxml2dom really should respect the lack of a namespace on the no_ns
element, which the current version unfortunately doesn't do. However,
wouldn't the correct serialisation of the document be as follows?

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

As for the first issue - the presence of the xmlns attribute in the
serialised document - I'd be interested to hear whether it is
considered acceptable to parse the serialised document and to find that
no non-null namespaceURI is set on the href element, given that such a
namespaceURI was set when the document was created. In other words, ...

document = libxml2dom.createDocument(None, "doc", None)
top = document.xpath("*")[0]
elem1 = document.createElementNS("DAV:", "href")
document.replaceChild(elem1, top)
elem2 = document.createElementNS(None, "no_ns")
document.xpath("*")[0].appendChild(elem2)
document.toFile(open("test_ns.xml", "wb"))

...as before, followed by this test:

document = libxml2dom.parse("test_ns.xml")
print "Namespace is", repr(document.xpath("*")[0].namespaceURI)

What should the "Namespace is" message produce?

Paul




More information about the Python-list mailing list