Schematron Validation With Lxml In Python: How To Retrieve Validation Errors?
I'm trying to do some Schematron validation with lxml. For the specific application I'm working at, it's important that any tests that failed the validation are reported back. The
Solution 1:
OK, so someone on Twitter gave me a suggestion which made me realise that I mistakenly got the reference to the schematron class all wrong. Since there don't seem to be any clear examples, I'll share my working solution below:
import StringIO
from lxml import isoschematron
from lxml import etree
defmain():
# Example adapted from http://lxml.de/validation.html#id2# Schema
f = StringIO.StringIO('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern id="sum_equals_100_percent">
<title>Sum equals 100%.</title>
<rule context="Total">
<assert test="sum(//Percent)=100">Sum is not 100%.</assert>
</rule>
</pattern>
</schema>
''')
# Parse schema
sct_doc = etree.parse(f)
schematron = isoschematron.Schematron(sct_doc, store_report = True)
# XML to validate - validation will fail because sum of numbers # not equal to 100
notValid = StringIO.StringIO('''\
<Total>
<Percent>30</Percent>
<Percent>30</Percent>
<Percent>50</Percent>
</Total>
''')
# Parse xml
doc = etree.parse(notValid)
# Validate against schema
validationResult = schematron.validate(doc)
# Validation report
report = schematron.validation_report
print("is valid: " + str(validationResult))
print(type(report))
print(report)
main()
The print statement on the report now results in the following output:
<?xml version="1.0" standalone="yes"?><svrl:schematron-outputxmlns:svrl="http://purl.oclc.org/dsdl/svrl"xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:schold="http://www.ascc.net/xml/schematron"xmlns:sch="http://www.ascc.net/xml/schematron"xmlns:iso="http://purl.oclc.org/dsdl/schematron"title=""schemaVersion=""><!--
--><svrl:active-patternid="sum_equals_100_percent"name="Sum equals 100%."/><svrl:fired-rulecontext="Total"/><svrl:failed-asserttest="sum(//Percent)=100"location="/Total"><svrl:text>Sum is not 100%.</svrl:text></svrl:failed-assert></svrl:schematron-output>
Which is exactly what I was looking for!
Post a Comment for "Schematron Validation With Lxml In Python: How To Retrieve Validation Errors?"