1. Miscellaneous new features

    This chapter covers miscellaneous new features in XQuery 3.0 and 3.1.

    1. Switch expression

      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>
      result:
      <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>
      • This example begins with a direct element constructor for people which contains an enclosed expression for the element content.
      • The enclosed expression contains a for clause which binds the /people/person nodes to the variable $i.
      • The for clause is followed by a let clause which assigns the value of the dept attribute of the person element to the $dept variable.
      • The first let clause is followed by a second let clause which assigns to the variable $translation the result of evaluating the switch expression with $dept as the input.
      • The return clause of the FLWOR expression uses a direct element constructor for person.
      • The direct element constructor contains an enclosed expression for the element content.
      • The enclosed expression contains a sequence of items consisting of a computed attribute constructor, followed by the name and age elements of the respective person.
      • The computed attribute constructor uses a constant to create an attribute named abteilung (the german translation of 'department')
      • An enclosed expression is used to compute the attribute value i.e. { $translation } of the abteilung attribute.