views:

3098

answers:

2

I am having trouble using the attribute XPath Selector in ElementTree, which I should be able to do according to the Documentation

Here's some sample code

XML

<root>
 <target name="1">
    <a></a>
    <b></b>
 </target>
 <target name="2">
    <a></a>
    <b></b>
 </target>
</root>

Python

def parse(document):
    root = et.parse(document)
    for target in root.findall("//target[@name='a']"):
        print target._children

I am receiving the following Exception:

expected path separator ([)
+2  A: 

Looks like findall only supports a subset XPath. See the mailing list discussion here

mjmarsh
+8  A: 

The syntax you're trying to use is new in ElementTree 1.3 the version shipped with python is 1.2.6

Florian Bösch