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
numberchild ofproduct, and your input document was validated, you can be sure that anumberchild will exist for eachproduct. 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
numberelement contains an integer value. This is useful, for example, if you write a query that returns results sorted bynumber. The query processor will know that thenumbervalues 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access