2012-06-09 46 views
2

我正在使用lxml schematron模塊驗證xml文檔。它運行良好,但我無法顯示驗證報告,該報告被設置爲屬性。我找不到如何將其作爲XML樹進行處理。schematron報告問題與python lxml

這裏我使用的代碼片段:

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報告樹嗎?

回答

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

好吧......我的例子就是這種情況。 –

3

人誰在這裏結束了也可能想看看下面的問題;第一個答案提供了一個非常明確的例子,說明如何使Schematron報告工作(發佈這個是因爲我找不到任何工作示例,並且我發現文檔也有些混淆)。這裏有:

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

+0

太棒了。解決了這個問題。我用report = schematron.validation_report替換了報告init,並完成了。 –