XML解析器验证报告
时间:2020-03-05 18:44:26 来源:igfitidea点击:
在文档中出现第一个错误之后,大多数XML解析器都会放弃。实际上,IIRC实际上是解析器"正式"规范的一部分。
我正在寻找可以打破该规则的东西。它应该采用给定的架构(假设有效的架构)和xml输入,并尝试在出现第一个错误之后继续执行,并针对每个错误引发一个事件,或者在完成后返回一个列表,因此我可以使用它来生成某种形式的文档中的错误报告。这个要求来自上面,所以让我们尝试将纯粹主义者"但是继续下去"的注释减少到最低限度。
我正在寻找一种可以评估文档是否格式正确以及文档是否符合架构的东西。理想情况下,它将这些错误视为不同类别的错误。我希望使用.Net解决方案,但也可以使用独立的.exe。如果我们知道使用其他平台的平台,请继续发布并发布,因为其他人可能会发现它很有用。
更新:
我希望我使用该文档的大多数文档都将格式正确。可能是&作为数据而不是在此处和此处的&amp包含在数据中,或者偶尔放错了标签。我不希望解析器能够从任何内容中恢复,只是尽最大努力继续前进。如果文档太不精打采,则应尽其所能吐出,并伴有某种"致命的,无法继续的"错误。否则,模式验证部分将非常容易。
解决方案
回答
In fact, IIRC, that's actually part of the 'official' spec for parsers.
官方不需要引用:)
fatal error [Definition:] An error which a conforming XML processor must detect and report to the application. After encountering a fatal error, the processor may continue processing the data to search for further errors and may report such errors to the application. In order to support correction of errors, the processor may make unprocessed data from the document (with intermingled character data and markup) available to the application. Once a fatal error is detected, however, the processor must not continue normal processing (i.e., it must not continue to pass character data and information about the document's logical structure to the application in the normal way).
我们可以将xmllint与restore选项一起使用。
回答
听起来我们可能想要TagSoup。它可能并不是我们真正想要的,但是就不良文档处理解析器而言,这是黄金标准。
回答
Xerces具有一项功能,我们可以将其设置为在发生致命错误后尝试并继续:
http://apache.org/xml/features/continue-after-fatal-error True: Attempt to continue parsing after a fatal error. False: Stops parse on first fatal error. Default: false Note: The behavior of the parser when this feature is set to true is undetermined! Therefore use this feature with extreme caution because the parser may get stuck in an infinite loop or worse.