Validate a LIXI2 Package Using Schematron Rules (for Python)
Schematron allows us to validate LIXI2 messages against business rules. While the LIXI2 schemas are excellent tools for validating message structure and providing a framework for standardised messaging between systems, Schematron gives us a way to define more complex business rules in a format that is distributable. Rather than providing a .pdf document to developers explaining the payload that is required by your system, a Schematron rule sheet can provide the same functionality in a machine readable format that can more easily be consumed by an automated system.
what is schematron? A language for making assertions about the presence or absence of patterns in XML documents.
what is schematron used for? Business rules validation, data reporting, general validation, quality control, quality assurance, firewalling, filtering, constraint checking, naming and design rules checking, statistical consistency, data exploration, transformation testing, feature extraction, house-style-rules checking
what makes schematron unique? Schematron is very simple (only five important elements), very powerful (it can express many kinds of constraints impossible in other schema languages), very diverse (it can be used for business rules, reports as well as the kinds of static constraints usually associated with schemas.)
How to validate a LIXI2 Message using Schematron
LIXI has created a tool (available as a component of our LIXI Python Package) to help you validate your LIXI2 packages against Schematron.
1) Install the LIXI Python package
Install the LIXI Python package using this command:
pip install lixi
2) Download a LIXI2 schema
Download the LIXI2 schema version that is appropriate for your LIXI2 message. Links to download LIXI2 schemas can be found here: LIXI standards Release History.
3) Prepare your Schematron rule sheet
You can write your own Schematron schema rules to meet your particular requirements. Alternately, sample Schematron schemas can be downloaded from our LIXILab Schematron Demo project (available to logged in members).
Here is an example of a Schematron schema file with only one rule:
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"> <sch:pattern id="Tutorial"> <sch:rule context="/Package/Content/Application/Liability"> <sch:assert test="(@Secured='Yes' and Security) or @Secured='No'"> If a liability is secured by an asset, the details of the security asset must be provided. </sch:assert> </sch:rule> </sch:pattern> </sch:schema>
4) Execute the Schematron validation
Use the following code snippet to validate your LIXI2 package against the Schematron rule sheet. Make sure to update the paths used in the code snippet to match your LIXI2 package file location, LIXI2 schema file location, and Schematron schema file location.
import lixi message = lixi.read_message( message_path="message.xml", schema_path="LIXI-CAL-2_6_22-Annotated.xsd" ) result = message.validate_schematron( schematron_schema_path="schematron.sch" ) print(result)
This script will first validate the LIXI2 package against the XSD, then execute the Schematron validation, providing two layers of validation. If the package fails either validation, an error message will be displayed on the terminal.
An introduction to the LIXI Python Package: Introducing the LIXI Python Package
The detailed package documentation includes a Quickstart Guide that takes you through all the key functionality (available to logged in users - any member or licensee can register for a free account here).
LIXI hold regular training sessions (free for members), at which you can get more information - see upcoming sessions here.
Finally, feel free to contact any member of the LIXI team (or use our contact form) and we will be happy to help with more information and a demonstration.
John Matthews, LIXI Technical Lead
First Published: February 26, 2020