XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 0596006349

1st edition, , O'Reilly Media, Inc.

Chapter 14: Static typing

Example 14-1. Typeswitch expressions
typeswitch ($myProduct)
  case element(*,prod:HatType) return xs:string($myProduct/size)
  case element(*,prod:ShirtType)
       return xs:string(concat($myProduct/size/@system, ": ",
                               $myProduct/size))
  case element(*,prod:UmbrellaType) return "none"
  default return "n/a"
Example 14-2. Binding variables to typeswitch expressions
typeswitch ($myProduct)
  case $h as element(*,prod:HatType) return xs:string($h/size)
  case $s as element(*,prod:ShirtType)
       return xs:string(concat($s/size/@system, ": ", $s/size))
  case element(*,prod:UmbrellaType) return "none"
  default return "n/a"
Example 14-3. Alternative to a typeswitch expression
if ($myProduct instance of element(*,prod:HatType))
then xs:string($myProduct/size)
else if ($myProduct instance of element(*,prod:ShirtType))
     then xs:string(concat($myProduct/size/@system, ": ", $myProduct/size))
     else if ($myProduct instance of element(*,prod:UmbrellaType))
          then "none"
          else "n/a"
Example 14-4. A query without a treat expression
if ($myProduct instance of element(*,prod:HatType))
then <p>The size is: {data($myProduct/size)}</p>
else ()
Example 14-5. A query with a treat expression
if ($myProduct instance of element(*,prod:HatType))
then
  <p>The size is: {data(($myProduct treat as element(*,prod:HatType))/size)}</p>
else ()
Example 14-6. A FLWOR with a type declaration
for $prod as element(*,ProductType) in doc("catalog.xml")/catalog/*
order by $prod/name
return $prod/name
Example 14-7. A quantified expression with a type declaration
every $number as element(*,xs:integer) in
    doc("catalog.xml")//number satisfies ($number > 0)
Datypic XQuery Services