[XML-SIG] Node as Element?

Alexandre Fayolle Alexandre.Fayolle@logilab.fr
Thu, 20 Dec 2001 18:31:51 +0100 (CET)


On Thu, 20 Dec 2001, Ian Sparks wrote:

> 
> This is so simple but it has me stumped.
> 
> If I do :
> 
> from xml.dom.ext.reader.Sax2 import Reader
> 
> ...setting up document & y here....
> 
>    for x in y.childNodes:
>         if x.getAttributeNS('','attribute_name') == 'foo':
>            do_something...
> 
> I get an error. 

try this: 

from xml.dom import Node
for x in y.childNodes:
    if x.nodeType == Node.ELEMENT_NODE:
        if x.getAttributeNS('','attribute_name') == 'foo':
            # do stuff


> This is where my Python knowledge is weak, I want to do :    
> 
> if (x as Element).getAttributeNS('type') == 'foo':
>    dosomething...
> 
> but I'm afraid that just shows my Delphi roots....What's the Python
> solution?

There is no need to cast in Python. The DOM way of looking at the type of
a Node is by using the nodeType attribute. 

Alexandre Fayolle
-- 
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org
Narval, the first software agent available as free software (GPL).