Sequence Types

A sequence type is used in a query to specify the expected type of a sequence of zero, one, or more items. When declaring functions, sequence types are used to specify the types of the parameters as well as the return value. For example, the function declaration:

declare function local:getProdNums ($catalog as element( )) as xs:integer*
  {$catalog/product/xs:integer(number)};

uses two sequence types:

  • element( ), to specify that the $catalog parameter must be one (and only one) element

  • xs:integer*, to specify that the return type of the function is zero to many xs:integer values

Sequence types are also used in many type-related expressions, such as the cast as, treat as, and instance of expressions. The syntax of a sequence type is shown in Figure 11-2.

Syntax of a sequence typeThe detailed syntax of < element-attribute-test > is shown in Figure 13-4.

Figure 11-2. Syntax of a sequence type[a]

Occurrence Indicators

An occurrence indicator can be used at the end of a sequence type to indicate how many items can be in a sequence. The occurrence indicators are:

? For zero or one items

* For zero, one, or many items

+ For one or many items

If no occurrence indicator is specified, it is assumed that the sequence can have one and only one item. For example, a sequence type of xs:integer matches one and only one atomic value of type xs:integer. A sequence type of xs:string* matches a sequence that is either the empty sequence or contains one or more atomic values of type ...

Get XQuery 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.