简体   繁体   English

python lxml的schematron报告问题

[英]schematron report issue with python lxml

I'm validating xml documents with the lxml schematron module. 我正在使用lxml schematron模块验证xml文档。 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. 我找不到如何将其作为XML树进行处理。

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. 关于这一点,lxml文档尚不清楚。 Can somebody help me getting the xml report tree? 有人可以帮助我获取xml报告树吗?

You need to set the Schematron class' store_report __init__(...) parameter to True (default: False). 您需要将Schematron类的store_report __init__(...)参数设置为True(默认值:False)。

IMHO the documentation is pretty clear on this point, see eg http://lxml.de/api/lxml.isoschematron.Schematron-class.html or 恕我直言,在这一点上,文档非常清晰,请参见例如http://lxml.de/api/lxml.isoschematron.Schematron-class.html

>>> 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.

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). 第一个答案提供了一个非常清晰的示例,说明如何使Schematron报告工作(发布此消息是因为我找不到任何有效的示例,而且我发现lxml文档也有些令人困惑)。 Here goes: 开始:

Schematron validation with lxml in Python: how to retrieve validation errors? 在Python中使用lxml进行Schematron验证:如何检索验证错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM