I have a Schematron document that uses XSLT 2.0 and I am looking for a method in python to validate a series of xmls using the Schematron rules.
I've tried lxml but it does not support XSLT 2.0 and I've also tried to use the saxonc api which seems to just crash when I try to initialize it.
Has anyone had any success processing XSLT 2.0 in python for Schematron validation?
I downloaded the latest Schxslt release 1.5.2 schxslt-1.5.2-xslt-only.zip
from https://github.com/schxslt/schxslt/releases/tag/v1.5.2 and run the following example Python program using Python 3.7 and Saxon-C HE 1.2.1 on Windows:
import saxonc
with saxonc.PySaxonProcessor(license=False) as proc:
print("Test Saxon/C on Python")
print(proc.version)
xslt30_processor = proc.new_xslt30_processor()
xslt30_processor.set_cwd(".")
xslt30_processor.transform_to_file(source_file="price-xslt2.sch", stylesheet_file="../../../schxslt-1.5.2/2.0/pipeline-for-svrl.xsl", output_file="price-compiled-saxon-c.xsl")
xslt30_processor.transform_to_file(source_file="books.xml", stylesheet_file="price-compiled-saxon-c.xsl", output_file="saxon-c-report-books.xml")
That runs fine and with the first transform_to_file
call produces an XSLT file price-compiled-saxon-c.xsl
which the second transform_to_file
call applies to the input sample and produces a validation report as saxon-c-report-books.xml
.
If you want to avoid the intermediary file then the following also works:
import saxonc
with saxonc.PySaxonProcessor(license=False) as proc:
print("Test Saxon/C on Python")
print(proc.version)
xslt30_processor = proc.new_xslt30_processor()
xslt30_processor.set_cwd(".")
compiled_schematron = xslt30_processor.transform_to_value(source_file="price-xslt2.sch", stylesheet_file="../../../schxslt-1.5.2/2.0/pipeline-for-svrl.xsl")
stylesheet_node = compiled_schematron.item_at(0).get_node_value()
xslt30_processor.compile_stylesheet(stylesheet_node = stylesheet_node)
xslt30_processor.transform_to_file(source_file="books.xml", output_file="saxon-c-report-3-books.xml")
The only unknown factor is my installation of Saxon-C 1.2.1, I can't tell whether it is identical to the latest release you can download from Saxonica as that is a year old and various bug reports on https://saxonica.plan.io/projects/saxon-c have led to some fixes I might have applied; unfortunately Saxonica so far never released a new maintenance release with all fixes.
If you run into problems running Saxon-C from Python, I guess it is better to open an issue or a question on their support forum with all minimal but complete details to reproduce it and I am sure they will help you out to tell you how to get up and running.