XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 1491915103

2nd edition, , O'Reilly Media, Inc.

Chapter 12: Prologs, Modules, and Variables

Please note that the book contains many inline examples and informal tables that are not provided here.

Example 12-1. A query prolog
xquery version "3.0";
declare default element namespace "http://datypic.com/cat";
declare boundary-space preserve;
declare namespace ord = "http://datypic.com/ord";
import schema namespace prod="http://datypic.com/prod"
                        at "prod.xsd";
declare function local:getProdNums
  ($catalog as element()) as xs:integer*
  {for $prod in $catalog/product
   return xs:integer($prod/number)};
Example 12-2. A query body
<h1>Order Report</h1>,
for $item in doc("order.xml")//item
order by $item/@num
return $item
Example 12-3. A library module (strings.xqm)
module namespace strings = "http://datypic.com/strings";
declare variable $strings:maxStringLength := 32;
declare function strings:trim($arg as xs:string?) as xs:string? {
  replace(replace($arg,'^\s+',''),'\s+$','')
};
Example 12-4. Private function
module namespace strings = "http://datypic.com/strings";
declare function strings:trim($arg as xs:string?) as xs:string? {
  if (strings:get-norm-length($arg) < $strings:trim-length)
  then substring($arg, 1, $strings:trim-length)
  else $arg
};
declare %private function strings:get-norm-length
  ($arg as xs:string?) as xs:integer {
  string-length(normalize-space($arg))
};
declare %private variable $strings:trim-length := 32;
Datypic XQuery Services