Validation from Python
The SaxonC Python interface allows schemas to be loaded into a PySaxonProcessor, and then to be used for validating instances, or for schema-aware XSLT and XQuery processing.
The main steps are:
-
Create a PySaxonProcessor using the constructor
proc = PySaxonProcessor(license=True). (Note that schema processing requires SaxonC-EE). -
Call the
new_xsd_compiler()method to get a PyXsdCompiler. 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). -
Construct a PyXsdSchema object, representing a compiled schema, by calling the
compile()method (using one of the keywordsxsd_text,xsd_nodeorxsd_fileto compile a schema from lexical string,PyXdmNodeobject or file, respectively). The resulting schema document is available to all applications run within the containingPySaxonProcessor. -
To validate an instance document, first call the
new_schema_validator()method on thePyXsdSchemaobject. This returns a PySchemaValidator. -
Set options on the
PySchemaValidatorto control the way in which a particular validation episode is performed, and then invoke itsvalidate()orvalidate_to_node()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. -
Validation errors are returned to the standard error listener. It is also possible to call the class property
validation_reportto return a validator report as aPyXdmNodeobject. -
It is possible to save a compiled schema in an SCM (schema component model) file using the
export_components()method on thePyXsdSchemaobject, and to import a previously-saved schema by callingimport_components()on thePyXsdCompilerobject. -
If the schema is to be imported into a schema-aware XSLT stylesheet, XQuery, or XPath expression, it can be supplied to the relevant
PyXslt30Processor,PyXQueryProcessor, orPyXPathProcessorvia itsuseSchema()method.
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.
Unlike Saxon versions prior to Saxon 13, compiling a schema does not make it globally
available to all applications using a given PySaxonProcessor. Each compiled schema is
distinct and can be used individually without interfering with other schemas. However, all
the schemas used within a PySaxonProcessor 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 PySchemaValidator can be used with the PyDocumentBuilder class to parse
and validate XML documents.