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:

  1. Create a SaxonProcessor using the constructor new SaxonProcessor(true) (schema processing requires SaxonC-EE). Then call the method newSchemaValidator() to create a new SchemaValidator.

  2. 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).

  3. Register and load a schema document by calling one of the methods registerSchemaFromString(), registerSchemaFromNode or registerSchemaFromFile, to load a schema from lexical string, XdmNode object or file, respectively. For the registerSchemaFromFile() 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 the exportSchema() method).

  4. To validate an instance document, call the validate() or validateToNode() method on the SchemaValidator object.

  5. Validation errors are returned to the standard error listener. It is also possible to call the method getValidationReport() to return a validator report as an XdmNode 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.