How do you switch on a string 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 your processor supports XQuery 1.1, then you can simply do:
switch ($room)
case "bathroom" return "loo"
case "kitchen" return "scullery"
default return "just a room"
XQuery doesn't have a function for switching on anything other than elements.
The first thing you do is convert your string to an element.
let $str := "kitchen"
let $room := element {$str} {}
Then just use typeswitch to do a normal switch:
return typeswitch($room)
case element(bathroom) return "loo"
case element(kitchen) return "scullery"
default return "just a room"
Please note, this may be a MarkLogic only solution.
Starting with XQuery 1.1, use switch:
http://www.w3.org/TR/xquery-11/#id-switch
switch ($animal)
case "Cow" return "Moo"
case "Cat" return "Meow"
case "Duck" return "Quack"
default return "What's that odd noise?"