Skip to main content

PySchematron

This is a library package for Schematron validation in Python.

Schematron is a schema language used to validate XML documents. A Schematron schema is defined as an XML containing various assertions to validate a target XML document. If the XML you wish to validate passes all the Schematron assertions, your XML is considered valid according to the Schematron schema. Complete validation results are offered using the Schematron Validation Report Language, a loose definition of an XML based validation report.

There are various versions of Schematron available. This library only supports the latest version of Schematron, ISO/IEC 19757-3:2020, with a few limitations (see below).

Currently, this library only supports a pure Python mode of Schematron validation. In this pure Python mode we load the Schematron into an internal representation and apply that to an XML. The advantage of such direct evaluation is that it offers superior performance compared to an XSLT transformation based evaluation. The disadvantage is that it only supports XPath expressions and does not support XSLT functions.

In the future we hope to expand this library with an XSLT transformation based processing. Unfortunately XSLT transformations require an XSLT processor, which is currently not available in Python for XSLT >= 2.0.

A few similar packages to this software in other languages are node-schematron in Javascript, and ph-schematron in Java.

For all XPath expressions this package uses the elementpath library supporting XPath 1.0, 2.0, 3.0 and 3.1 selectors.

Please note that, as of this writing, this package only supports Python 3.12. Older Python versions are not supported due to missing functionality (Python syntax primarily). Newer versions will be supported in due time.

Python API

To use the Python API, install the project like any other Python project, e.g. using pip install pyschematron.

After that you can use:

from pyschematron import validate_document

result = validate_document(<xml_document.xml>, <schematron_schema.sch>)

svrl = result.get_svrl()
is_valid = result.is_valid()

To process multiple documents with the same Schematron schema, you can use:

from pyschematron import validate_document

documents = [...]
schema = <schema.sch>

results = validate_documents(documents, schema)

For more examples, or examples on how to use different parts of the API, please see the demo_* files in the scripts directory.

Command Line Interface

To use the command line interface, first install the application using pip: pip install pyschematron. Afterwards, you can use the command pyschematron to validate your documents. Use pyschematron --help to see the command line options.

Functionality

This library offers a basic implementation of Schematron using a pure Python “direct mode” evaluation method.

Direct mode evaluation

The direct mode evaluation allows for basic validity checks using all XPath functionality of Schematron.

When applied to a document, the direct mode evaluation follows this procedure to validate a document:

  1. Read in the Schematron from either a document or a string. In this phase the document is loaded into an AST (abstract syntax tree). All <includes /> are resolved and inlined into the AST. All <extends /> are loaded but not fully resolved at this stage.

  2. Recreate the AST without abstract patterns and rules. In this phase we process the AST to create a concrete set of patterns and rules. All <extends /> are resolved, abstract patterns are instantiated, and redundant abstract rules and patterns are removed.

  3. Phase selection, we limit the AST to only include patterns and phases limited to the selected phase.

  4. Query binding, we determine the query binding language to use. This library only supports xslt, xslt2, xslt3, xpath, xpath2, xpath3, and xpath31, where all xslt variations are limited to XPath expressions only.

  5. Apply the bound schema to an XML document to validate.

Custom functions

With the current direct mode evaluation method, custom XSLT functions in your Schematron (<xsl:function>) are not supported. Custom Python functions are supported however. View the demo_custom_functions.py in the scripts directory for examples.

Compliance

The direct mode evaluation supports most of the ISO/IEC 19757-3:2020 standard, with a few exceptions. All Schematron specific elements are supported, except for XSLT elements.

In terms of attributes, the @documents attribute of the <assert /> tag is not supported. Furthermore, @icon, @see, @fpi, @flag, and @role are loaded but not used.

Note that the ISO Schematron applies rules to:

  • Elements (*)

  • Attributes (@*)

  • Root node (/)

  • Comments (comment())

  • Processing instructions (processing-instruction())

But it does not apply rules to text nodes.

If there are any problems, please open a Github issue.

Metadata

Release files for pyschematron 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyschematron 1.2.2
File Size Uploaded
pyschematron-1.2.2.tar.gz 494.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyschematron 1.2.2
File Interpreter ABI Platform
pyschematron-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 567.6 kB

Release files / pyschematron-1.2.2.tar.gz

Download URL pyschematron-1.2.2.tar.gz
Size 494.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fcf30eb0ead3e69bbb8d6cc19769bb1a9760e35ae4d4db0de7ae7929b511c1d5
BLAKE2b-256 checksum
How to use checksums
6325a9badbee288551f1378ab620f1cb0b93614e894b5936cd85f009c118a18b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.34.2

Release files / pyschematron-1.2.2-py3-none-any.whl

Download URL pyschematron-1.2.2-py3-none-any.whl
Size 73.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
916660082242b217fa8c891b7b2664d0bdd4047cb9a1d7be0647b9d2d17a939a
BLAKE2b-256 checksum
How to use checksums
43b1f3d116836622a10ebf1168805aead5bc3d26b308c5b4b4c65e029624fd8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.34.2

Release history Release notifications | RSS feed

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.18

2 release files

1.1.17

2 release files

1.1.16

2 release files

1.1.15

2 release files

1.1.11

2 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page