Saxonica.com

Validation of Source Documents

With Saxon-SA, source documents may be validated against a schema. Not only does this perform a check that the document is valid, it also adds type information to each element and attribute node in the document to identify the schema type against which it was validated. It may also expand the source document by adding default values of elements and attributes.

If the val option is specified on the command line for com.saxonica.Query or com.saxonica.Transform, then the principal source document to the query or transformation is schema-validated, as is every document loaded using the doc() or document() function. Saxon will look among all the loaded schemas for an element declaration that matches the outermost element of the document, and will then check that the document is valid against that element declaration, reporting a fatal error if it is not. The loaded schemas include schemas imported statically into the query or stylesheet using import schema or xsl:import-schema, schemas referenced in the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attributes of the source document itself, and schemas loaded by the application using the addSchema method of the Configuration object.

As an alternative to -val, the option -vlax may be specified. This validates the document if and only if an element declaration can be found. If there is no declaration of the outermost element in any loaded schema, then it is left as an untyped document.

When invoking transformations or queries from the Java API, the equivalent of the -val option is to call the method setSchemaValidation(Validation.STRICT) on the Configuration object. The equivalent of -vlax is setSchemaValidation(Validation.LAX).

It is possible from the Java API, or from a URIResolver, to request validation selectively for some documents and not for others. This is done by returning an AugmentedSource object. The AugmentedSource wraps an ordinary JAXP Source object, and contains a validation property indicating whether strict or lax validation should be applied.

Saxon's standard URIResolver uses this technique if it has been enabled (for example by using -p on the command line). With this option, any URI containing the query parameter ?validation=strict (for example, doc('source.xml?val=strict')) causes strict validation to be requested for that document, while ?validation=lax requests lax validation, and ?validation=strip requests no validation.

Next