Structure of an XQuery Module
If you're familiar with other query languages such as SQL, OQL, you'll notice that XQuery has some similarities, and you'll probably find some of the syntax to be quite familiar. The term "query module" as used in this article means a "query unit". You can think of an XQuery module as having three parts (see Figure 1
- Namespace and Schema Declarations [optional]
- Function Definitions [optional]
- Query Expressions
|Figure 1: The three parts of a query module.|
The first two parts together are called the query prolog
. The first part can consist of both namespace declarations and schema import statements. The example in Figure 1
defines a namespace prefix xsd
mapped to the URI "http://www.w3.org/2000/01/XMLSchema"
. The second part contains function definitions. This is the place in a query module to define custom functions. The example in Figure 1
defines a factorial function that accepts an integer parameter and returns its factorial.
The third part of the query contains query expressions. Expressions are the key to XQuery. The example in Figure 1 uses an expression called an Element Constructor, which, as the name suggests, constructs an element named Results. The example invokes the factorial function with a parameter value 10, and the result becomes the content of the Value element.