Validation of Source Documents

With Saxon-EE, 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 option -val:strict 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:strict, the option -val:lax 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.

It is also possible to set the configuration option SCHEMA_VALIDATION to the value Validation.STRICT (1) or Validation.LAX (2). However, using a configuration-wide setting is not recommended because validation is usually applied to some input documents and not others.

When documents are built using the DocumentBuilder in the s9api interface, or the DocumentBuilder in the Saxon.Api interface on .NET, validation may be controlled by setting the appropriate options on the DocumentBuilder.

On Java interfaces that expect a JAXP Source object it is possible to request validation by supplying an AugmentedSource. This consists of a Source and a set of options, including validation options; since AugmentedSource implements the JAXP Source interface it is possible to use it anywhere that a Source is expected, including as the object returned by a user-written URIResolver.

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?validation=strict')) causes strict validation to be requested for that document, while ?validation=lax requests lax validation, and ?validation=strip requests no validation.

XSLT 3.0 provides a standard way of requesting validation for individual source documents, using the validation and type attributes of the xsl:source-document instruction.