ElementTree and XPATH

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dayzman@hotmail.com

    #1

    ElementTree and XPATH

    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

  • Fredrik Lundh

    #2
    Re: ElementTree and XPATH

    <dayzman@hotmai l.com> wrote:
    [color=blue]
    > 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?[/color]

    the XPath subset supported by ElementTree is documented here:



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



    </F>



    Comment

    • Istvan Albert

      #3
      Re: ElementTree and XPATH

      dayzman@hotmail .com wrote:
      [color=blue]
      > it seems to be invalid syntax if I give "/a/b[0]" to the findall()
      > method. Does anyone know the correct syntax?[/color]

      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.

      Comment

      • Martijn Faassen

        #4
        Re: ElementTree and XPATH

        Istvan Albert wrote:[color=blue]
        > dayzman@hotmail .com wrote:
        >[color=green]
        >> it seems to be invalid syntax if I give "/a/b[0]" to the findall()
        >> method. Does anyone know the correct syntax?[/color]
        >
        >
        > 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.[/color]

        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. :)




        Regards,

        Martijn

        Comment

        Working...