4

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 ([)
2
  • findtext seems like an odd way of using xpath, especially considering stackoverflow.com/questions/8692/how-to-use-xpath-in-python shows a different method. Commented Jan 1, 2011 at 17:06
  • Merydith: Please, use a full complain XPath engine like lxml and not basic ElementTree API. Commented Jan 1, 2011 at 17:12

4 Answers 4

3

I might be wrong but I dont think that the "./bar[@key='false']" notation works in Python 2.5.5 (or at least not with the ElementTree that comes with it). I have observed the same problem in Python 2.6.5, but it does work in Python 2.7.1. I guess you will have to use another library or try the "experimental" GAE with Python 2.7.

Sign up to request clarification or add additional context in comments.

1 Comment

Looking at the source of ElementTree as included with Python 2.5, it does seem that attribute selectors aren't supported.
2

This XPath will select the bar nodes whose key attribute is equal to false:

/foo/bar[@key='false']

If the current context node is the foo node then this will also work:

./bar[@key='false']

3 Comments

I thought that was the syntax, but it returns and error (see update to original post).
@Will Merydith - Are you sure the current context node is foo? Try the first example.
I'm pretty certain because the rest of my code is working (grabbing lots of data from this xml). If I try the first example I get the error: "SyntaxError: cannot use absolute path on element"
1

Based on the answer here, the XPath selector functionality was not implemented in ElementTree until version 1.3, which ships with Python 2.7, as @cdemers said.

Comments

0

"[@attrib]" which you are using is only introduced in ElementTree 1.3: http://effbot.org/zone/element-xpath.htm

which was introduced only to Python 2.7:

https://docs.python.org/2/library/xml.etree.elementtree.html

As other people here mentioned, you need to get this attribute differently or upgrade Python for that code to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.