schemaon报告问题与python lxml

时间:2012-06-09 17:47:20

标签: python lxml schematron

我正在使用lxml schematron模块验证xml文档。它运行良好,但我无法显示验证报告,该报告被设置为属性。我找不到如何将它作为XML树处理。

这里是我使用的代码片段:

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  

lxml文档在这一点上并不清楚。有人可以帮助我获取xml报告树吗?

2 个答案:

答案 0 :(得分:3)

您需要将Schematron类的store_report __init__(...)参数设置为True(默认值:False)。

恕我直言,关于这一点,文件非常清楚,参见例如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.

答案 1 :(得分:3)

到此结束的人也可能想看看下面的问题;第一个答案提供了一个非常明确的示例,说明如何使Schematron报告工作(发布这个因为我无法找到任何工作示例,我找到了 lxml 文档也有点混乱)。这是:

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