schematron report issue with python lxml
Asked Answered
T

2

2

I'm validating xml documents with the lxml schematron module. It works well but I can't display the validation report, which is set as a property. I can't find how to process it as an XML tree.

Here the snippet of the code I use:

xdoc = etree.parse("mydoc.xml")
# schematron code removed for clarity
f = StringIO.StringIO('''<schema>...</schema>''')
sdoc = etree.parse(f)
schematron = isoschematron.Schematron(sdoc, store_schematron=True, store_xslt=True, store_report=True)
if schematron.validate(xdoc):
    print "ok"
else:
     tprint "ko"

report = isoschematron.Schematron.validation_report

>>> type(report)
<type 'property'>
>>> dir(report)
['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']
>>> report.__doc__
'ISO-schematron validation result report (None if result-storing has\n        been turned off).\n  

The lxml documentation is not clear on this point. Can somebody help me getting the xml report tree?

Tham answered 9/6, 2012 at 17:47 Comment(0)
K
3

You need to set the Schematron class' store_report __init__(...) parameter to True (default: False).

IMHO the documentation is pretty clear on this point, see e.g. http://lxml.de/api/lxml.isoschematron.Schematron-class.html or

>>> help(Schematron):
class Schematron(lxml.etree._Validator)
 |  An ISO Schematron validator.
 |  
 |  ...
 |  With ``store_report`` set to True (default: False), the resulting validation
 |  report document gets stored and can be accessed as the ``validation_report``
 |  property.
Keek answered 10/10, 2012 at 7:17 Comment(1)
well ... this was the case in my sample.Tham
T
3

People who ended up here may also want to take a look at the question below; the first answer provides a pretty clear example of how to make Schematron reporting work (posting this because I wasn't able to find any working examples, and I found the lxml documentation to be somewhat confusing as well). Here goes:

Schematron validation with lxml in Python: how to retrieve validation errors?

Thackeray answered 26/11, 2014 at 15:15 Comment(1)
great. that solves the issue. I replaced the report init by report = schematron.validation_report and it's done.Tham

© 2022 - 2024 — McMap. All rights reserved.