[Fwd: Re: [Zope] test for the existence of a nodeValue]
Thanks Dieter, But I still cant get it to work. I've changed my get_attributes.py script to this: if not attobject: return '' version = attobject.get_viewable() nodes = version.content.documentElement.getElementsByTagName(attname) if not nodes: return '' nodeValue = nodes[0].childNodes[0].nodeValue if not nodeValue: return "none" return nodeValue and my zpt is like this: <span id="section_subheader" tal:define="isadditional python:here.get_uclattribute(attobject, 'ucl_additional_name')" tal:condition="python: isadditional !='none'" tal:attributes="id python:here.get_uclattribute(attobject, 'ucl_additional_name_color')"><span tal:content="python:here.get_uclattribute(attobject, 'ucl_additional_name') or nothing" tal:omit-tag="">Website Subheader</span></span> But I get the following error: Error Type: IndexError error Value: list index is out of range and the traceback says "Module None, line 7 in get_attributes" is the cause. Hope you can help. Jon
Jon Bowlas wrote at 2005-8-2 19:08 +0100:
... I'm pretty new to python and zope but would like to know how I can adapt this script below to test to see if nodeValue actually contains a value and if it doesn't then I would like to return a default value I can check for using my zpt that references this script:
You probably should work through the Python tutorial you will find on "http://python.org".
... if not nodes:
return ''
return nodes[0].childNodes[0].nodeValue
Try:
nodeValue = nodes[0].childNodes[0].nodeValue if not nodeValue: return "someFault" return nodeValue
-- Dieter
Jon Bowlas wrote at 2005-8-4 10:31 +0100:
... But I still cant get it to work. I've changed my get_attributes.py script to this:
if not attobject: return '' version = attobject.get_viewable() nodes = version.content.documentElement.getElementsByTagName(attname) if not nodes: return '' nodeValue = nodes[0].childNodes[0].nodeValue if not nodeValue: return "none" return nodeValue ... But I get the following error: Error Type: IndexError error Value: list index is out of range
"IndexError -- list index is out of range" tells you that this is not a "nodeValue" problem. Almost surely, you got a node without "childNodes". Then "node.childNodes[0]" will result in an "IndexError". Thus, you will need to check for this situation (in a way similar to checks for an empty "nodes" list).
and the traceback says "Module None, line 7 in get_attributes" is the cause.
This should tell you in which line of "get_attributes" the problem was. As your mail agent decided to reformat the code (oh this Outlook :-( ), and you did not number the line, I cannot tell you -- but I guessed above (and probably not too bad)... -- Dieter
Many thanks Dieter, works a treat. I'm working through the python tutorial now. -----Original Message----- From: Dieter Maurer [mailto:dieter@handshake.de] Sent: 04 August 2005 18:58 To: me@jonbowlas.com Cc: zope@zope.org Subject: Re: [Fwd: Re: [Zope] test for the existence of a nodeValue] Jon Bowlas wrote at 2005-8-4 10:31 +0100:
... But I still cant get it to work. I've changed my get_attributes.py script to this:
if not attobject: return '' version = attobject.get_viewable() nodes = version.content.documentElement.getElementsByTagName(attname) if not nodes: return '' nodeValue = nodes[0].childNodes[0].nodeValue if not nodeValue: return "none" return nodeValue ... But I get the following error: Error Type: IndexError error Value: list index is out of range
"IndexError -- list index is out of range" tells you that this is not a "nodeValue" problem. Almost surely, you got a node without "childNodes". Then "node.childNodes[0]" will result in an "IndexError". Thus, you will need to check for this situation (in a way similar to checks for an empty "nodes" list).
and the traceback says "Module None, line 7 in get_attributes" is the cause.
This should tell you in which line of "get_attributes" the problem was. As your mail agent decided to reformat the code (oh this Outlook :-( ), and you did not number the line, I cannot tell you -- but I guessed above (and probably not too bad)... -- Dieter
participants (2)
-
Dieter Maurer -
Jon Bowlas