• +49-(0)721-402485-12
Ihre Experten für XML, XQuery und XML-Datenbanken

Type query

For the query of the type of a sequence value, XQuery provides the two expressions instance of and typeswitch. The excerpt from the grammar is as follows:

InstanceofExpr

::=

Expr instance of SequenceType
TypeswitchExpr::=typeswitch ( Expr )
  CaseClause+
  default ($ VarName)?
  return ExprSingle
CaseClause::=case ($ VarName as)? SequenceType return ExprSingle

The instance of operator returns the true value in case the type of the first operand matches the data type to be checked (or is derived from it). The integer 4711 is accepted as integer and as decimal number and also as general item, as the following example demonstrates:

let $a := xs:integer(4711)
where $a instance of xs:integer
and $a instance of xs:decimal
and $a instance of item()
return
<Text>This text will appear.</Text>

The type testing is not only possible for atomic types, but also for all sequence types:

<Text>blah blah</Text> instance of element(*, xs:string)

A series of type testings can be combined in a typeswitch expression. A typeswitch expression corresponds to the switch instruction known from many programming languages, except for the fact that not the value, but the type of the selection expression decides on the selected variant. A typeswitch could also be simulated by a series of instance of expressions embedded in conditional expressions – however, there is a difference in the static type testing.

The following example returns the rate of remuneration for overtime hours depending on the occupational group. For example, overtime hours for doctors are remunerated with €25, for nursing staff only with €15. Here, the $a variable is bound to an element of the substitution group of Employee:

typeswitch ($e)
case element(*, Doctor_T) return 25
case element(*, Nurse_T) return 15
case element(*, Technician_T) return 20
case element(*, Secretary_T) return 10
default return 0

In general a typeswitch expression consists of a manifestation of a sequence type (in the example scenario above an employee bound to the $e variable) and a list of cases which shall be tested stepwise regarding compatibility. The order is crucial, so that the first case of a possible type compatibility determines the expression for the calculation of the return value. If in no explicitly indicated case a type match can be obtained, the expression is evaluated in the default clause and used as return value.

If the return value in a case or default clause depends on the sequence to be examined, it can be accessed by additionally indicating a variable which reflects the original value of the selection expression with regard to the currently examined type. The following expression returns a special feature of the respective occupational groups:

typeswitch ($e)
case $x as element(*, Doctor_T)
return <Feature>$x/SpecialField</Feature>
case $x as element(*, Nurse_T)
return <Feature>$x/Certificate</Feature>
case $y as element(*, Technician_T)
return <Feature>$y/Competence</Feature>
case element(*, Secretary_T)
return <Feature/>
default return <Error/>

An additional variable is introduced for the appropriate occupational groups. For secretaries and the default case this is not necessary. The scope of application of an additionally introduced variable includes the evaluation of the expression of the appropriate case, so that the variables must not be referenced outside. Therefore, it is also permitted to use the same identifier in several case branches within a typeswitch expression.

 

Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>