• Login or register

discuss.effbot.org

  • Popular
  • Recent
  • XPath Support in ElementTree (effbot.org)
    1 point by effbot 1 year ago
    • 3 comments
  • 1 point by JoshEnglish 4 months ago 0 children

    I can't get the last() predicate to work.

    Sample:

    from elementtree import ElementTree as ET

    text = """<term>
    <month id="september"/>
    <month id="october"/>
    <month id="novemeber"/>
    </term>"""

    tree= ET.fromstring(text)

    print tree.find('month[last()]')

    Raises an Invalid Predicate syntax error.

    • link
    • reply
  • 1 point by rogern 1 year ago 1 child

    Hi,
    I'm testing ElementTree 1.3 a3 and wnat to use the ElementPath to replace the dom getElementsByTagName. After reading and applying the recommendation to resolve {ns}tag into ns_prefix:tag, i try to perform a findall and it doesn't work. i have an element with the following content : <w:pict><v:shape>...</v:shape>....</w:pict>, so i try : myElt.findall('.//v:shape'). It always give me that errors: selector.append(ops[token[0]](next, token))
    KeyError: ':' .
    I try to debug and find that the xpath_tokenizer is matching ':' as a token, but in this article i do not see the mention of ':' that can be a token.

    I don't know if there is a workaround to allow tagname in the form ns_prefix:tag for the xpath support in elementtree. I'm about to migrate from minidom to a pull parser-like api, and want to preserve the tagname i use with the dom. Thanks in advance for any tip.
    Roger

    • link
    • reply
    • 1 point by effbot 1 year ago 0 children

      If you're using the "prefix:tag" form in XPath expressions in ET 1.3, you need to pass in a dictionary that tells the XPath engine what the prefix means, e.g.

      myElt.findall('.//v:shape', {'v': 'http://somethingsomething'})

      What prefix the file actually uses is irrelevant.

      • link
      • reply
  • Widget
  • Recent Comments
  • Leaders
Powered by