Schema Processing 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
and call itsgetSchemaManager()
method to get aSchemaManager
. -
If required, set options on the
SchemaManager
to control the way in which schema documents will be loaded. -
Load a schema document by calling the
load()
method, which takes a JAXP Source object as its argument. The resulting schema document is available to all applications run within the containingProcessor
. -
To validate an instance document, call the
newSchemaValidator()
method on theSchemaManager
object. -
Set options on the
SchemaValidator
to control the way in which a particular validation episode is performed, and then invoke itsvalidate()
method to validate an instance document.
Note that additional schemas referenced from the xsi:schemaLocation
attributes within the source documents will be loaded as necessary. 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
.
Although the API is defined in such a way that a SchemaValidator
is created for a
particular SchemaManager
, in the Saxon implementation the schema components that are available
to the validator are not only the components within that schema, but all the components
that form part of any schema registered with the Processor
(or indeed, with
the underlying Configuration
).
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.