Validation using s9api
The s9api interface allows schemas to be loaded into a Processor, and then to be used for validating instances, or for schema-aware XSLT and XQuery processing.
The main steps are:
-
Create a net.sf.saxon.s9api.Processor.
-
Call the newSchemaCompiler() method to get an XsdCompiler. It is possible to get multiple schema compilers, or to use a single schema compiler to compile multiple schemas.
-
If required, set options on the
XsdCompilerto control the way in which schema documents will be loaded. It is possible to set the XSD version (to 1.0 or 1.1) and to provide user-written callbacks for reporting schema errors and for URI resolution. -
Construct an XsdSchema object, representing a compiled schema, by calling one of the various
compile()methods. The resulting schema document is available to all applications run within the containingProcessor. -
To validate an instance document, first call the newValidator() method on the
XsdSchemaobject. This returns a SchemaValidator. -
Set options on the
SchemaValidatorto control the way in which a particular validation episode is performed, and then invoke its validate() method to validate an instance document. Options available include the ability to control whether additional schema components can be referenced from the instance document usingxsi:schemaLocationattributes, and control over the reporting of any validity errors encountered. It is also possible to set aDestinationfor the validated document: this will be the original source document, augmented with defaulted elements and attributes as defined in the schema, and with type annotations indicating the types associated with each element and attribute. These type annotations are particularly relevant when the instance document is then processed using a schema-aware query or stylesheet. -
It is possible to save a compiled schema in an SCM (schema component model) file using the method exportComponents() on the
XsdSchemaobject (typically supplying aSerializeras the destination), and to import a previously-saved schema by calling XsdCompiler.importComponents(). -
If the schema is to be imported into a schema-aware XSLT stylesheet, XQuery, or XPath expression, it can be supplied to the relevant
XsltCompiler,XQueryCompiler, orXPathCompilervia itsuseSchema()method.
Note that additional schemas referenced from the xsi:schemaLocation
attributes within the source documents will be loaded as necessary: see
Using
xsi:schemaLocation. By default a target
namespace is ignored if there is already a loaded schema for that namespace; Saxon makes
no attempt to load multiple schemas for the same namespace and check them for
consistency. This behaviour can be changed using the configuration option MULTIPLE_SCHEMA_IMPORTS.
Unlike Saxon versions prior to Saxon 13, compiling a schema does not make it globally
available to all applications using a given Processor. Each compiled schema is
distinct and can be used individually without interfering with other schemas. However, all
the schemas used within a Processor must be consistent. Consistency
is defined in the version 4.0 language specifications: in simplified terms, it means that
the same name must not be used in different schemas to refer to different element or type
declarations.
The SchemaValidator
implements the Destination
interface, which means it can be used to receive input from any process that writes to a
Destination, for example an XSLT transformation or an XQuery query. The
result of validation can also be sent to any Destination, for example an
XSLT transformer.