[XML-SIG] help - attributes namespace - is this a bug in PyXML

Andrew Clover and at doxdesk.com
Tue Aug 17 03:56:11 CEST 2004

Mike Brown <mike at skew.org> wrote:

> I'm not very experienced with using minidom but that's surprising to me.

Probably because Ajay isn't using minidom :-)

>   from xml.dom.minidom import parseString
>   inputString = '<appel:RULE appel:empty="" appel:full="hi"/>'
>   doc = parseString(inputString)
>   doc.childNodes[0].getAttribute('appel:connective')

>    ''

> I would've expected None

'' is correct in this case. getAttribute returns an empty string if no 
attribute is found as per DOM Level 1 spec. It is getAttributeNode that 
returns None (null) when the attribute is not found.

> it seems weird that '' and u'' mean different things

They don't. Python binds the DOMString type to strings in general, so 
both unicode and narrow strings can be used. (Though it is usually best 
to use unicode, and definitely a bad idea to be putting non-ASCII 
characters in narrow binary strings.) It just happens that minidom 
returns a narrow empty string for attribute-not-found; it could just as 
easily be u''.

> Therefore you should not be using
> getAttribute()/getAttributeNS() to test for existence of an attribute.

Indeed. This can be useful when an attribute value should act as if 
defaulting to the empty string.

> What you should be doing is using hasAttribute or hasAttributeNS.

Yep. Alternatively getAttributeNode can also do the job.

Andrew Clover
mailto:and at doxdesk.com

More information about the XML-SIG mailing list