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 ([)
Will Curran
  • 6,959
  • 15
  • 59
  • 92
  • findtext seems like an odd way of using xpath, especially considering http://stackoverflow.com/questions/8692/how-to-use-xpath-in-python shows a different method. – Andrew T Finnell Jan 01 '11 at 17:06
  • Merydith: Please, use a full complain XPath engine like `lxml` and not basic ElementTree API. –  Jan 01 '11 at 17:12

4 Answers4

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.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
chuckshop
  • 51
  • 1
  • 5
  • Looking at the source of ElementTree as included with Python 2.5, it does seem that attribute selectors aren't supported. – Nick Farina Mar 03 '12 at 17:53
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']
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I thought that was the syntax, but it returns and error (see update to original post). – Will Curran Jan 01 '11 at 16:08
  • @Will Merydith - Are you sure the current context node is `foo`? Try the first example. – Oded Jan 01 '11 at 16:10
  • 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" – Will Curran Jan 01 '11 at 16:14
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.

Community
  • 1
  • 1
Ken Bellows
  • 6,711
  • 13
  • 50
  • 78
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.

aaviram
  • 71
  • 3