[XML-SIG] Node as Element?

Fred L. Drake, Jr. fdrake@acm.org
Thu, 20 Dec 2001 11:28:10 -0500 (EST)


Ian Sparks writes:
 >    for x in y.childNodes:
 >         if x.getAttributeNS('','attribute_name') == 'foo':
 >            do_something...
 > 
 > I get an error. 

  I'm going to guess that you're getting an AttributeError, and that x
is *not* an Element node, but a Text node that contains whitespace
from between elements.  You should be able to use this:

    for x in y.childNodes:
        if (x.nodeType == Node.ELEMENT_NODE
             and x.getAttributeNS('','attribute_name') == 'foo'):
            do_something...

  If you're not getting an AttributeError, then you'll need to tell us
more about the error you're getting.

 > 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?

  If x really is an Element, you don't need to do anything.  If it
isn't an element, you probably want to know about it, and the test I
added above should do the trick.


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Zope Corporation