Why Use Schemas with Queries?

There are a number of advantages of using schemas with queries:

Predictability

If an input document has been validated against a schema, its structure and data content are predictable. If the schema says that there will always be a number child of product, and your input document was validated, you can be sure that a number child will exist for each product. You do not need to check for its existence before you use it in an expression.

Type information for use in expressions

The schema provides type information to the query processor about the values in the instance document. For example, it can tell us that the number element contains an integer value. This is useful, for example, if you write a query that returns results sorted by number. The query processor will know that the number values should be sorted as integers and not strings, and will therefore sort 100 after 99. If they were sorted as strings, 100 would come before 99.

Identification of query errors

Schemas can be used in static analysis to determine the expected type of an expression. Using schemas, you might discover errors in the query that were not otherwise apparent. Schemas can also help you debug your queries more quickly and easily by providing more useful error messages. To use a SQL analogy, you wouldn't want a SQL statement that had a misspelled column name to come back with nothing instead of raising an error. Without XML schemas, this is exactly what your XQuery queries will ...

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.