Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you switch on a string in XQuery?

I have an external variable coming in as a string and I would like to do a switch/case on it. How do I do that in xquery?

like image 362
Sixty4Bit Avatar asked Sep 17 '08 17:09

Sixty4Bit


1 Answers

Just use a series of if expressions:

if ($room eq "bathroom") then "loo"
else if ($room eq "kitchen")  then "scullery"
else "just a room"

Using a typeswitch is hiding what you are really doing.

Which of these methods is most efficient will depend on the XQuery processor you are using. In an ideal world it should only be a matter of taste, as it should be down to the optimizer to select the appropriate method, but if performance is important it is worth benchmarking both versions. I would be very surprised if a processor optimized the node construction out of your example, and didn't optimize my example to a specialized switch.

like image 79
Oliver Hallam Avatar answered Oct 02 '22 12:10

Oliver Hallam