Type Declarations

Some expressions, namely FLWORs, quantified expressions, and global variable declarations, allow the use of a type declaration to force the static type of an expression. A type declaration uses the keyword as, followed by a sequence type. The sequence types used in these expressions follow the syntax described in Chapter 11.

Type Declarations in FLWORs

Sequence types can be used in the for and let clauses of FLWORs to declare the type of variable being bound. In this case, the type declaration appears immediately after the variable name, as in Example 14-6.

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

The sequence type element(*,ProductType) is specified as the type of the variable $prod. Without the type declaration, this query might raise a type error using a processor that implements static typing, if the schema allows the possibility of catalog having children that don't themselves have name children. The type declaration serves as a way of telling the processor, "I know that all the children of catalog will be valid elements of type ProductType, which all have name children, so don't raise a static error. If it turns out I'm wrong, you can raise a dynamic error later."

With the type declaration, this error checking is postponed to evaluation time. When the query is evaluated, if the value of $prod does not have the type ProductType, a type error is raised. Note that the purpose of the sequence type specification is not to filter out items that do not conform, but to raise type errors when nonconforming items are encountered.

Unlike type declarations in function signatures, no conversions take place. Untyped atomic data won't be converted to the required type, numeric type promotion won't happen—in fact, you won't even get atomization. The only difference allowed between the actual type of the value and the declared type is subtype substitution: if the required type is xs:decimal, for example, the supplied value can be xs:integer, but it can't be a node containing an xs:integer.

Type Declarations in Quantified Expressions

Quantified expressions also allow sequence types to be specified for variables, using a similar syntax, as in Example 14-7.

Example 14-7. A quantified expression with a type declaration

every $number as element(*,xs:integer) in
    doc("catalog.xml")//number satisfies ($number > 0)

In this case, the $number variable is given the sequence type element(*,xs:integer). If any of the items returned by the expression doc("catalog.xml")//number do not match that sequence type, a type error is raised.

Important

Declaring sequence types for variables in quantified expressions, or for clauses, is not supported in XPath 2.0.

Type Declarations in Global Variable Declarations

An optional type declaration can be specified in a global variable declaration, as in:

declare variable $firstNum as xs:integer
  := data(doc("catalog.xml")//product[1]/number);

which associates $firstNum with the static type xs:integer. As with other type declarations, this type declaration does not change or cast the value in any way. It is simply used to reassure the processor that the typed value of $firstNum will always be an integer, so it can be used in arithmetic operations, for example. This is especially useful for external variables, since their static type cannot be determined any other way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.163.175