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

Mike Brown mike at skew.org
Mon Aug 16 20:08:26 CEST 2004

Ajay wrote:
> also getAttribute("appel:connective") returns " ", ie it is not None but
> when i print it out thats what i get

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

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

You're right: an empty byte string is returned in that case. I would've 
expected None, too.

Given that an existing attribute results in a unicode object being returned, 

>>> doc.childNodes[0].getAttribute('appel:empty')
>>> doc.childNodes[0].getAttribute('appel:full')

it seems weird that '' and u'' mean different things, but I am guessing the 
intent was DOM conformance, and DOM demands that a string be returned (DOM is 
a poorly designed API, by the way), and minidom's implementation is probably 
supposed to return u'' in both cases. Therefore you should not be using
getAttribute()/getAttributeNS() to test for existence of an attribute.

What you should be doing is using hasAttribute or hasAttributeNS. The fact 
that these methods are not documented at 
http://www.python.org/doc/2.3.4/lib/dom-element-objects.html is a 
documentation bug.

> funnily getAttribute("appel:connective") for an element thats doesn't have
> the attribute "appel:connective" still passes the test
> if element.getAttribute("appel:connective") != None

Per PEP 8 (coding style guide on python.org) always use "is None" or "is not 
None" rather than "== None" or "!= None".

Again, a simple test shows why:

>>> '' != None
>>> '' == None

> so how can i retrieve an attribute of type "appel:connective", ie, prefixed
> by the uri appel
> and getAttributeNS doesn't work either. same as for getAttribute

I think you realize this, but appel is not a URI, it is a prefix. 
http://www.w3.org/2001/02/appelv1 is a URI. (Well, technically, I think folks 
are now saying that if it's being used as a namespace name, then it's not a 
URI, it's just a string that is required to match the URI syntax)

Anyway, again, you're right, and I'd offer the same explanation as for

>>> doc.childNodes[0].getAttributeNS('http://www.w3.org/2001/02/appelv1', 'connective')

More information about the XML-SIG mailing list