>>> import xml.dom
>>> d = xml.dom.getDOMImplementation ().createDocument ("", "root", None)
>>> d.toxml()
'<?xml version="1.0" ?>\n<root/>'
>>> root = d.childNodes[0]
>>> root.appendChild(d.createElementNS("uri:hello", "hello:hi"))
<DOM Element: hello:hi at 0x930d48>
>>> d.toxml()
'<?xml version="1.0" ?>\n<root><hello:hi/></root>'
>>>
So if XML parsers are supposed to take care of things for me, where's the xmlns:hello attribute on root? I thought maybe PyXML would do it, but apparently not. I would accept I needed to do invoke a special DOMImplementation if (a) it were anywhere documented what the available features are (though the DOM spec makes me think it's just there for compliance and not actually used, so this isn't really a solution at all), and (b) there weren't a createElementNS method in the standard implementation or DOM, though yes, I understand namespaces aren't technically part of XML proper--but then they make the DOM dependent on namespaces?
Small consolation:
>>> d.getElementsByTagNameNS("uri:hello", "hi")
[<DOM Element: hello:hi at 0x930d48>]
But if they can make that work--even realizing the : in hello:hi means hello is the prefix of a qualified name!--why doesn't it do the xmlns attributes for me?
Comments
comment
I guess the “out” is that
toxml()is non-standard to start with. So blaming “XML Parsers” in general is wrong, the problem is with this particular XML parser offering a non-standard feature that has a bug.DOM levels 1 and 2 don’t cover the conversion of a DOM Document into XML. I’m pretty sure the currently technically correct way to turn a DOM tree into an XML document is to pass it through an XSLT stylesheet. (Of course, XSLT sucks).
DOM level 3 (currently in draft) adopts the canonical form for XML, and has a Load and Save specification, which rather than providing a simple
toxml()method, looks suspiciously like it was written to match Java’s IO model.comment
Oh-ho, I didn’t even think about
toxmlbeing nonstandard, but of course, it is.Yes, DOM Load and Save looks very Java-y. I’m not sure I’m happy about that, but then, PyXML already complies somewhat. It has a DOMImplementationLS and its createDOMWriter method… it just doesn’t do anything, as there are no DOMWriter implementations.
I suppose I’d just write my own
toxmlmethod if I needed it, but everything’s only in the null namespace at the moment anyway.