Defect1ve
Defect1ve

Reputation: 48

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.

Upvotes: 1

Views: 134

Answers (1)

kjhughes
kjhughes

Reputation: 111521

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.

Upvotes: 2

Related Questions