Robustness

Queries should be able to handle all possible combinations of values of input data. This includes handling any potential variations in the data, and considering the impact of missing or empty values.

Handling Data Variations

It is important to consider variations in the input documents that may cause incorrect results or dynamic errors. Some common problems occur when:

Sequences of multiple items appear where only one item was expected

For example, the expressions $prod[name eq "Floppy Sun Hat"] and substring($prod/name, 1, 30) raise an error if there is more than one name child. The expression $prod/name != "Floppy Sun Hat" evaluates to true if two name children exist and either one is not equal to Floppy Sun Hat.

Zero items appear where one was expected

For example, the expression $prod/price - $prod/discount returns the empty sequence if there is no discount element.

Values do not conform to the expected type

For example, the expression max($prod/number) raises a type error if the product number is N/A instead of an integer.

Values are outside the expected range

Especially zero and negative numbers where a positive number was expected.

You should not assume that because an input document is validated by a schema it must be exactly as you expect. A schema can validate, for example, that a required element is present, but other assumptions might be made that cannot be validated by a schema. For example, the constraint "if the discounted attribute is true, a discount child ...

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.