Given:
<foo>
<bar key="true">text1</bar>
<bar key="false">text2</bar>
<bar key="true">text3</bar>
<bar key="true">text4</bar>
</foo>
I want to get the text for the bar element where the key attribute = "false".
My application is Python 2.5.5 on GAE. The XML is not true xml, but I can load it as an ElementTree and fetch data normally.
Code example:
result = urllib2.urlopen(url).read()
xml = ElementTree.fromstring(result)
str = xml.find("./bar").attrib['key']
to get the first value. I've tried various xpath queries that I think should work but I've obviously got the syntax wrong.
UPDATE:
str = xml.findtext("./bar[@key='false']")
Throws error:
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/etree/ElementPath.py", line 93, in __init__
"expected path separator (%s)" % (op or tag)
SyntaxError: expected path separator ([)