0

I want to select elements based on an a range of attribute values. I have this without the 'or' functionality:

elem = root.iterfind('.//container/image/[name="foo"]..')

I would like something in the line of:

elem = root.iterfind('.//container/image/[name="foo" OR name="bar"]..')

How can this be acheived?

Rolle
  • 2,900
  • 5
  • 36
  • 40
  • 3
    Does this answer your question? ['OR' operator in XPath predicate?](https://stackoverflow.com/questions/2211693/or-operator-in-xpath-predicate) – Chillie Jul 01 '21 at 20:15

1 Answers1

2

Not in xpath in ElementTree (because of limited xpath support).

However, you could use lxml...

elem = root.xpath('.//container/image[name="foo" or name="bar"]')

That selects image elements that have a child name element with the value foo or bar. I wasn't sure what you were trying to select because the predicate wasn't preceded by an element name.

Or if you were using something that supported XPath 2.0 (like the python module of Saxon/C) you could test against a sequence of values...

elems = xpathproc.evaluate('.//container/image[name=("foo","bar")]')
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95