Chapter 12. Prologs, Modules, and Variables
This chapter covers the structure of queries in more detail. It discusses the query prolog and its various declarations. It then describes how to assemble queries from multiple modules, declare global variables, and define external functions.
Structure of a Query: Prolog and Body
An XQuery query is made up of two parts: a prolog and a body. The query prolog is an optional section that appears at the beginning of a query. The prolog can contain various declarations that affect settings used in evaluating the query. This includes namespace declarations, imports of schemas, variable declarations, function declarations, and other setting values. In a query module of any size, the prolog is actually likely to be much larger than the body.
Example 12-1 shows a query with a prolog containing several different types of declarations.
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
)};
The query body is a single expression, but that expression can consist of a sequence of one or more expressions that are separated by commas. Example 12-2 shows a query body that contains a sequence of two expressions, a constructed ...
Get XQuery, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.