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.
CodePudding user response:
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.