Search code examples
pythonxml.etree

Can .findall() match multiple values in python etree?


Is there a way to match multiple elements in a tree using .findall()?

I would like to do this:

trees = log.findall('element1' or 'element2')

This is my work around (which works in my case because I don't have both e1 and e2 in the same XML):

trees = log.findall('element1')
if not trees:
    trees = log.findall('element2')

I am parsing XML files that have similar structures but different names. C# allows "element1 | element2" matching.


Solution

  • No, you can't. C# appears to be using XPath expressions, but the ElementTree XPath support for XPath queries is too limited and does not include the support for this.

    You can use or to pick your second search if the first is empty:

    trees = log.findall('element1') or log.findall('element2')
    

    because an empty result is false-y.

    The alternative is to use lxml, an ElementTree API implementation on top of libxml2, which supports all of the XPath 1.0 spec. Then you can do:

    log.xpath('(.//element1|.//element2)')