0

I have a Debian 6 setup that I won't touch too much.

$ python --version Python 2.6.6 $ dpkg --list|grep lxml ii python-lxml 2.2.8-2 

Im trying to access with XPath like this:

import xml.etree.ElementTree as ET root = ET.parse("a.xml") for e in root.findall('.//rec/data/field[@name="id"]'): print(e.tag + " " + e.attrib.get('name')) 

I got an error

Traceback (most recent call last): File "a.py", line 4, in <module> for e in root.findall('.//rec/data/field[@name="id"]'): File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 647, in findall return self._root.findall(path) File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 355, in findall return ElementPath.findall(self, path) File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 198, in findall return _compile(path).findall(element) File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 176, in _compile p = Path(path) File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 93, in __init__ "expected path separator (%s)" % (op or tag) SyntaxError: expected path separator ([) 

Is this XPath syntax not supported by python-lxml 2.2.8 ?

What version support this and is compatible with python 2.6 ?

2
  • Your title is about lxml but your code use xml. Also you code works in Python 2.7 Not sure whether it's a Python 2.6 only issue. Commented Nov 15, 2013 at 7:22
  • Confirmed. ElementTree in python 2.6 does not support searching for attributes [@attribute] syntax. See stackoverflow.com/questions/15753714/… Commented Nov 15, 2013 at 7:26

1 Answer 1

1

The code is using ElementTree, not lxml.

findall method of ElementTree supports only subset of xpath. (XPath support in ElementTree).

[@attrib="value"] is also supported in ElementTree API 1.3. But ElementTree API is updated to 1.3 in Python 2.7.


Replace following line:

import xml.etree.ElementTree as ET 

with:

import lxml.etree as ET 

to use lxml.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.