【问题标题】:schematron report issue with python lxmlschematron 报告 python lxml 的问题
【发布时间】:2012-06-13 08:39:18
【问题描述】:

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

这里是我使用的代码的 sn-p:

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 报告树吗?

【问题讨论】:

    标签: python lxml schematron


    【解决方案1】:

    您需要将 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.
    

    【讨论】:

    • 嗯……我的示例就是这种情况。
    【解决方案2】:

    来到这里的人可能还想看看下面的问题;第一个答案提供了一个非常清楚的示例,说明如何使 Schematron 报告工作(发布此内容是因为我无法找到 any 工作示例,并且我找到了 lxml 文档也会有些混乱)。如下:

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

    【讨论】:

    • 太棒了。这解决了这个问题。我用 report = schematron.validation_report 替换了报告初始化,它完成了。
    猜你喜欢
    • 2021-04-23
    • 2011-07-17
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2013-09-26
    • 2013-02-12
    • 1970-01-01
    • 2012-06-10
    相关资源
    最近更新 更多