PySchemaValidator

class PySchemaValidator

Bases: object

A PySchemaValidator represents a factory for validating instance documents against a schema.

Creates a new instance, keeping a reference to its PySaxonProcessor

Parameters:

processor (PySaxonProcessor)

clear_parameters()

Clear all parameters set on the processor

clear_properties()

Clear all properties set on the processor

cwd

Property represents the current working directory

export_schema(file_name)

Export a precompiled Schema Component Model containing all the components (except built-in components) that have been loaded

Parameters:

file_name (str) – The file name that will be used for the saved SCM

get_output_file()

Get the name of the output file that will be used by the validator.

Returns:

The output file name used for use by the validator

Return type:

str

register_schema(**kwds)

Deprecated since version 13.0.

Register a schema supplied as file name, schema text, or XDM node

Keyword Arguments:
  • xsd_text (str) – A string containing the schema document.

  • xsd_file (str) – The path to the XSD file to be compiled.

  • xsd_node (PyXdmNode) – The Schema document to be compiled.

remove_parameter(name)

Remove the parameter given by name from the PySchemaValidator. The parameter will not have any effect on the validation if it has not yet been executed.

Parameters:

name (str) – The name of the schema parameter

Returns:

True if the removal of the parameter has been successful, False otherwise.

Return type:

bool

set_cwd(cwd)

Set the current working directory

Parameters:

cwd (str) – current working directory

set_lax(lax)

The validation mode may be either strict or lax. The default is strict; this method may be called to indicate that lax validation is required. With strict validation, validation fails if no element declaration can be located for the outermost element. With lax validation, the absence of an element declaration results in the content being considered valid.

Parameters:

lax (bool) – True if validation is to be lax, False if it is to be strict

set_output_file(output_file)

Set the name of the output file that will be used by the validator.

Parameters:

output_file (str) – The output file name for use by the validator

set_parameter(name, value)

Set the value of a parameter for the Schema validator.

Parameters:
  • name (str) – the name of the schema parameter, as a string. For a namespaced parameter use Clark notation '{uri}local'.

  • value (PyXdmValue) – the value of the parameter, or None to clear a previously set value.

set_property(name, value, encoding=None)

Set a property specific to the processor in use.

Parameters:
  • name (str) – The name of the property

  • value (str) – The value of the property

  • encoding (str) – The encoding of the name argument. Argument can be omitted or None. If not specified then the platform default encoding is used.

Example

PySchemaValidator: set serialization properties (names start with ‘!’ i.e. name “!method” -> “xml”) ‘o’: output file name, ‘dtd’: Possible values ‘on’ or ‘off’ to set DTD validation, ‘resources’: directory to find Saxon data files, ‘s’: source as file name, ‘string’: Set the source as xml string for validation. Parsing will take place in the validate method ‘report-node’: Boolean flag for validation reporting feature. Error validation failures are represented

in an XML document and returned as a PyXdmNode object

‘report-file’: Specify value as a file name string. This will switch on the validation reporting feature,

which will be saved to the file in an XML format

‘verbose’: boolean value which sets the verbose mode to the output in the terminal. Default is ‘on’ ‘element-type’: Set the name of the required type of the top-level element of the document to be validated.

The string should be in clark notation {uri}local

‘lax’: Boolean to set the validation mode to strict (False) or lax (‘True’)

set_source_node(source)

Set the source node for validation

Parameters:

source (PyXdmNode) – the source node to be validated

validate(**kwds)

Validate an instance document by a registered schema.

Keyword Arguments:
  • xml_text (str) – A string containing the schema document.

  • file_name (str) – The path to the XSD file to be compiled.

  • xdm_node (PyXdmNode) – The Schema document to be compiled.

One and only one of the keyword arguments should be supplied. If the source document to be validated was supplied using set_source_node(), then no argument needs to be supplied.

Raises:
  • Exception – If invalid arguments are provided.

  • PySaxonApiError – if the source document is found to be invalid, or if error conditions occur that prevented validation from taking place (such as failure to read or parse the input document).

validate_to_node(**kwds)

Validate an instance document by a registered schema, returning the validated document.

Keyword Arguments:
  • xml_text (str) – A string containing the schema document.

  • file_name (str) – The path to the XSD file to be compiled.

  • xdm_node (PyXdmNode) – The Schema document to be compiled.

One and only one of the keyword arguments should be supplied. If the source document to be validated was supplied using set_source_node(), then no argument needs to be supplied.

Returns:

The validated document returned to the calling program as a PyXdmNode

Return type:

PyXdmNode

Raises:
  • Exception – If invalid arguments are provided.

  • PySaxonApiError – if the source document is found to be invalid, or if error conditions occur that prevented validation from taking place (such as failure to read or parse the input document).

validation_report

Get the validation report

Returns:

The Validation report result from the Schema validator

Return type:

PyXdmNode

Raises:
  • PySaxonApiError – if the source document is found to be invalid, or if error conditions occur

  • that prevented validation from taking place (such as failure to read or parse the input document) –