This chapter covers miscellaneous new features in XQuery 3.0 and 3.1.
The switch expression is a familiar construct to programmers.
A switch expression allows the selection of one of a choice of several expressions to be evaluated based upon an input value.
Switch expressions are an alternative to using a series of if else statements.
A switch expression begins with the keyword switch, followed by an expression in parentheses known as the switch operand expression.
The switch operand expression is followed by one or more case clauses, with one or more case operand expressions, followed by a default clause.
example: switch expression
<people> { for $i in /people/person let $dept := $i/@dept let $translation := switch ( $dept ) case "sales" return "verkauf" case "marketing" return "marketing" case "accounting" return "buchhaltung" default return "" return <person> { (attribute abteilung { $translation } , $i/name , $i/age) } </person> } </people>
<people> <person abteilung="verkauf"> <name>mary</name> <age>20</age> </person> <person abteilung="marketing"> <name>cindy</name> <age>25</age> </person> <person abteilung="verkauf"> <name>john</name> <age>40</age> </person> <person abteilung="buchhaltung"> <name>peter</name> <age>35</age> </person> </people>