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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With