XPath get one attribute or another?


I have a Python XML XPath expression ancestor-or-self::*[@foo]/@foo, and I need to modify it to get attribute @foo if it exists otherwise get attribute @bar.

I’ve tried to use or operator similar to condition like [@foo or @bar], but got an expression error.

Asked By: Defect1ve



XPath 1.0

For a correct XPath_Prefix, this XPath,

  • XPath_Prefix/@*[name()='foo' or name()='bar'][1]

will select the foo attribute if available; otherwise, the bar attribute.

Answered By: kjhughes
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.