Validation from C++
Schema validation can be controlled using the SaxonC C++ interface. The SaxonC C++ API is integrated across the range of Saxon XML processing interfaces, and provides the internal interface upon which the Python and PHP APIs are built.
The C++ interface allows schemas to be loaded into a SchemaValidator, and then to be used for validating instances, or for schema-aware XSLT and XQuery processing.
The main steps are:
-
Create a SaxonProcessor using the constructor
new SaxonProcessor(true)
(schema processing requires SaxonC-EE). Then call the methodnewSchemaValidator()
to create a new SchemaValidator. -
Set any options required on the
SchemaValidator
to control the way in which schema documents will be loaded, and the way a validation episode is invoked and performed (for example, the source document, current working directory and schema parameters). -
Register and load a schema document by calling one of the methods
registerSchemaFromString()
,registerSchemaFromNode
orregisterSchemaFromFile
, to load a schema from lexical string,XdmNode
object or file, respectively. For theregisterSchemaFromFile()
method the file on disk can either be a schema document in source XSD format, or a compiled schema in Saxon-defined SCM format (as produced using theexportSchema()
method). -
To validate an instance document, call the
validate()
orvalidateToNode()
method on theSchemaValidator
object. -
Validation errors are returned to the standard error listener. It is also possible to call the method
getValidationReport()
to return a validator report as anXdmNode
object.
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 schema, 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 SaxonProcessor
.
The SchemaValidator
can be used with the DocumentBuilder
class to parse
and validate XML documents.