xml.etree.ElementTree and XPath

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xkenneth

    xml.etree.ElementTree and XPath

    All,

    Can I execute XPath queries on ElementTree objects ignoring the
    namespace? IE './node' instead of './{http://namespace.com}n ode'.

    Is there any support for XPath and Minidom?

    Regards,
    Ken
  • Stefan Behnel

    #2
    Re: xml.etree.Eleme ntTree and XPath

    xkenneth wrote:
    Can I execute XPath queries on ElementTree objects ignoring the
    namespace? IE './node' instead of './{http://namespace.com}n ode'.
    The XPath support in ET is very limited. You can use lxml.etree instead, which
    has full support for XPath 1.0, i.e. you can do

    tree.xpath('//*[local-name() = "node"]')



    Or you can do the iteration yourself, i.e.

    for el in tree.iter(): # or tree.getiterato r():
    if isinstance(el.t ag, basestring):
    if el.tag.split('} ', 1)[-1] == "node":
    print el.tag

    which works in both ET and lxml.etree.

    Stefan

    Comment

    Working...