[XML-SIG] question about setAttributeNS()

Andrew Clover and-xml at doxdesk.com
Thu Jan 8 18:20:14 EST 2004


Keith Beattie <KSBeattie at lbl.gov> wrote:

> When adding an attribute within a new namespace to an element, you need to 
> make two calls to setAttributeNS() in order for the namespace definition to 
> be added to the element (one for the new namespace and one for the new 
> attribute).

Not really, in DOM API terms. If you do:

  x= document.createElement('x')
  x.setAttributeNS('newNS', 'new:attr', 'value')

Then x.getAttributeNodeNS('newNS', 'attr').namespaceURI already evaluates
to 'newNS', despite the lack of an xmlns:new attribute to support it.

The only problem comes when you serialise this. Most Python implementations
don't automatically write the xmlns:new="newNS" attribute you would need for
a serialised version to be namespace-well-formed. And that's perfectly
reasonable because the standard they are based on (DOM Level 2 Core) has
absolutely nothing to say about how documents should be serialised.

This changes in DOM Level 3 (currently only supported by pxdom). If you use
a default Level 3 LS serialiser object, eg.:

  document.implementation.createLSSerializer().writeToString(document)

then the output must have the required extra attributes for namespace-well-
formedness added to it automatically. The same is true with the new Level 3
Core method Document.normalizeDocument().

(You can disable this behaviour by calling setParameter('namespaces', False)
on the LSSerializer.config or Document.domConfig concerned.)

> This is different than createElementNS() which adds the namespace
> attribute for you, when creating a new element in a new namespace.

? This is not part of the standard DOM API, and I know of no Python
implementation that does it. What software are you using?

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/



More information about the XML-SIG mailing list