senywork7
senywork7

Reputation: 3

Xpath - multiple "or conditions" with if else

Using the below XML example, what's the best way to write the Xpath3 expression

If fruit is apple and quantity is less than 12 or if fruit is watermelon and quantity is less than 2) then return "in-store shopping" otherwise "online"`

<table>
     <fruit>Apple</fruit>
     <quantity>5</quantity>
</table>

I tried below but it didn't work. I got an error saying "Error: Expected 'then' token in conditional if expression"

if(//fruit= "apple" and //quantity<12) or if(//fruit= "watermelon" and //quantity<2)
then "in-store  shopping" else "online"

Upvotes: 0

Views: 122

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

The grammar is more like maths than like English.

if (/table[(fruit="apple" and quantity<12) 
            or (fruit="watermelon" and quantity<2)
          ]) 
then "in-store shopping" 
else "online"

As @kjhughes says, you aren't going to get it right by guessing. You need to do some reading and find some reference materials that suit your learning style.

Note that if you have more than one table, you need to be very careful to ensure that the conditions on fruit and quantity are satisfied in the same table, not in different tables. I've tried to steer you in that direction.

Upvotes: 0

Related Questions