ElementTree and XPATH

Discussion in 'Python' started by dayzman@hotmail.com, Dec 9, 2004.

  1. Guest

    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
    , Dec 9, 2004
    #1
    1. Advertising

  2. <> wrote:

    > 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>
    Fredrik Lundh, Dec 9, 2004
    #2
    1. Advertising

  3. wrote:

    > 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.
    Istvan Albert, Dec 9, 2004
    #3
  4. Istvan Albert wrote:
    > wrote:
    >
    >> 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
    Martijn Faassen, Dec 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerard Flanagan

    XPath-like filtering for ElementTree

    Gerard Flanagan, Dec 26, 2005, in forum: Python
    Replies:
    1
    Views:
    431
    Fredrik Lundh
    Dec 27, 2005
  2. Andrew Lonie

    Elementtree 1.3 and xpath

    Andrew Lonie, Jan 10, 2008, in forum: Python
    Replies:
    0
    Views:
    293
    Andrew Lonie
    Jan 10, 2008
  3. xkenneth

    xml.etree.ElementTree and XPath

    xkenneth, Oct 18, 2008, in forum: Python
    Replies:
    1
    Views:
    525
    Stefan Behnel
    Oct 18, 2008
  4. Kee Nethery
    Replies:
    12
    Views:
    2,042
    Stefan Behnel
    Jun 27, 2009
  5. Jason Cooper
    Replies:
    1
    Views:
    446
    Jason Cooper
    Mar 9, 2012
Loading...

Share This Page