Read status from failed Relax NG validation in HXT
Validating an XML file in Haskell works fine with HXT and Relax NG, apart from one thing: how can I get the result?
With the following code, the XML file xmlFilename
gets validated against the Relax NG scheme rngFilename
. In case of an error, an error is output to stderr
, and the evaluation continues.
v <- runX
( readDocument
[ withRemoveWS yes -- remove redundant whitespace
, withValidate no -- don't validate source by DTD
] xmlFilename
>>>
-- validate source by Relax NG
validateDocumentWithRelaxSchema [] rngFilename
)
In case of an error, the variable v
holds the following information according to the hxt-relaxng documentation:
in case of validation errors, an empty document with status information in the root [is output]
The resulting tree with a faulty document really holds a status
(and module
) attribute:
NTree (XAttr "module") [NTree (XText "validate document with Relax NG schema") []],
NTree (XAttr "status") [NTree (XText "2") []]
No开发者_运维百科w the questions:
How can I check the output of validateDocumentWithRelaxSchema
whether there was a validation error?
Is there a predefined function I can use for that (but haven't found yet)?
Ok, I found the answer myself:
HXT error handling is located in Text.XML.HXT.Arrow.XmlState.ErrorHandling with the interesting function getErrStatus
.
v <- runX
( readDocument
[ withRemoveWS yes -- remove redundant whitespace
, withValidate no -- don't validate source by DTD
] xmlFilename
>>>
-- validate source by Relax NG
validateDocumentWithRelaxSchema [] rngFilename
>>>
getErrStatus
)
case v of
--severity [0]=[c_ok]
[0] -> --continue processing
--severity: [1]=[c_warn], [2]=[c_err], [3]=[c_fatal], else=something_really_really_bad_happened
_ -> --do error handling
精彩评论