Validation from PHP
Schema validation can be controlled using the SaxonC PHP interface.
The PHP 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
SchemaValidatorto 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(),registerSchemaFromNodeorregisterSchemaFromFile, to load a schema from lexical string,XdmNodeobject 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 theSchemaValidatorobject. -
Validation errors are returned to the standard error listener. It is also possible to call the method
getValidationReport()to return a validator report as anXdmNodeobject.
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.