Type Checking in XQuery

Because XQuery is a strongly typed language, an XQuery processor verifies that all items are of the appropriate type and raises type errors when they are not. There are two phases to processing a query: the static analysis phase and the dynamic evaluation phase, both of which have type-checking components.

The Static Analysis Phase

During the static analysis phase, the processor checks the query itself, along with any related schemas, for static errors, without regard to the input documents. It is roughly equivalent to compiling the query; that is, checking for syntax errors and other errors that will occur regardless of the input document. The processor raises static errors during the static analysis phase. Examples of static errors include:

  • Syntax errors, such as invalid keywords or mismatched brackets

  • Referring to a variable or calling a function that has not been declared

  • Using namespace prefixes that are not declared

Some implementations support an optional static typing feature, which means that they evaluate the types of expressions in a query during the static analysis phase. This allows errors in the query to be caught early and more reliably, and can help optimize queries. A number of expressions, functions and syntactic constructs are available solely to support static typing. These are discussed in Chapter 14.

Implementations that don't claim to support the static typing feature might also do static analysis in order to reduce the amount of runtime type checking needs. It's always a good idea to declare the types of your variables, function parameters, and function return types to give the processor as much information as possible.

The Dynamic Evaluation Phase

During the dynamic evaluation phase, the processor checks the query again, this time with the data from the input document. Some expressions that did not result in errors during the analysis phase will in fact result in errors during the evaluation phase. For example, the expression sum(doc("catalog.xml")//number) might pass the static analysis phase if number is untyped; the processor has no way of knowing whether all the contents of the number elements will be numeric values. However, it will raise a dynamic error in the evaluation phase if any of the number elements contains a value that cannot be cast to a numeric type, such as the string abc.

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

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