我正在使用ph-schematron验证我的XML文件.我能够正确验证文件,但找不到如何生成有关断言失败的报告.
这是我的上下文(兴趣点):
这是我的Schematron模式:
xmlns="http://purl.oclc.org/dsdl/schematron"
xmlns:iso="http://purl.oclc.org/dsdl/schematron"
queryBinding='xslt2'
schemaVersion='ISO19757-3'>
Test ISO schematron file. Introduction modeThis pattern validates call activities with RESERVATION operation code.
err1
err2
这是我的Java代码:
public static boolean validateXMLViaPureSchematron(@Nonnull final String aSchematronFilePath, @Nonnull final File aXMLFile) throws Exception {
final SchematronResourcePure schematronResourcePure = SchematronResourcePure.fromClassPath(aSchematronFilePath);
IPSErrorHandler errorHandler = new CollectingPSErrorHandler();
schematronResourcePure.setErrorHandler(errorHandler);
final boolean validSchematron = schematronResourcePure.isValidSchematron();
if (!validSchematron) {
throw new IllegalArgumentException("Invalid Schematron!");
}
final Source streamSource = new StreamSource(aXMLFile);
final EValidity schematronValidity = schematronResourcePure.getSchematronValidity(streamSource);
return schematronValidity.isValid();
}
我可以通过调用schematronResourcePure.getSchematronValidity(streamSource)看到验证的结果,但是我想查看(一个报告就足够了)哪些规则失败了(err1或err2).我已经阅读了有关SVRL的文章,但我不知道如何生成报告.
谢谢.
解决方法:
只需调用applySchematronValidationToSVRL即可获取完整的SVRL(Schematron验证结果列表)文档.您可以查询失败的断言或报告.
仅打印失败的断言的代码示例:
SchematronOutputType schematronOutputType = schematronResourcePure.applySchematronValidationToSVRL(streamSource);
List failedAsserts = schematronOutputType.getActivePatternAndFiredRuleAndFailedAssert();
for (Object object : failedAsserts) {
if (object instanceof FailedAssert) {
FailedAssert failedAssert = (FailedAssert) object;
System.out.println(failedAssert.getText());
System.out.println(failedAssert.getTest());
}
}
标签:validation,schematron,xml,java
来源: https://codeday.me/bug/20191027/1945303.html