ElementTree and XPATH

D

dayzman

Hi,

I'm using ElementTree from effbot (http://effbot.org/zone/element.htm)
and I'm having some problems finding nodes that have the same name. I
know in XPATH, we can use an index to identify which node we need, but
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?
Any help will be much appreciated.

Cheers,
Michael
 
F

Fredrik Lundh

I'm using ElementTree from effbot (http://effbot.org/zone/element.htm)
and I'm having some problems finding nodes that have the same name. I
know in XPATH, we can use an index to identify which node we need, but
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?

the XPath subset supported by ElementTree is documented here:

http://effbot.org/zone/element-xpath.htm

for more extensive support, see Ken Rimey's PDIS toolkit:

http://pdis.hiit.fi/pdis/download/

</F>
 
I

Istvan Albert

it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?

I think the proper mindset going in should be that
elementtree does not support xpath but that
there are some handy constructs that resemble
the location steps of xpath.

Sometimes it takes very little work to achieve what
you want directly with python. In your case you could
probably use:

findall("/a/b")[0]

to the same effect.

Istvan.
 
M

Martijn Faassen

Istvan said:
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?


I think the proper mindset going in should be that
elementtree does not support xpath but that
there are some handy constructs that resemble
the location steps of xpath.

The lxml Pythonic wrapper of libxml2 which aims (among others) to build
an elementtree API compatible interface will indeed extend that API and
offer XPath support. Of course it's all not done yet. :)

http://codespeak.net/mailman/listinfo/lxml-dev
http://codespeak.net/svn/lxml/trunk/

Regards,

Martijn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top