ph-schematron 验证错误消息

ph-schematron validation error message

我正在使用 ph-schematron 来验证我的 XML 文件。我能够正确验证文件,但找不到如何生成有关失败断言的报告。

这是我的上下文(兴趣点):

<bpmn:extensionElements>
    <activiti:in sourceExpression="RESERVATION" target="OPERATION_CODE"/>
    <activiti:in sourceExpression="true" target="IS_SYNC"/>
</bpmn:extensionElements>

这是我的 Schematron 模式:

<iso:schema
    xmlns="http://purl.oclc.org/dsdl/schematron"
    xmlns:iso="http://purl.oclc.org/dsdl/schematron"
    queryBinding='xslt2'
    schemaVersion='ISO19757-3'>
    <iso:title>Test ISO schematron file. Introduction mode</iso:title>
    <iso:ns prefix='bpmn' uri='http://www.omg.org/spec/BPMN/20100524/MODEL'/>
    <iso:ns prefix='activiti' uri='http://activiti.org/bpmn'/>

    <iso:let name="callActivity" value="bpmn:definitions/bpmn:process/bpmn:callActivity"/>
    <iso:let name="inSourceExpression" value="child::activiti:in/sourceExpression"/>
    <iso:let name="outSourceExpression" value="child::activiti:out/sourceExpression"/>
    <iso:let name="inTarget" value="child::activiti:in/target"/>
    <iso:let name="outTarget" value="child::activiti:out/target"/>

    <!-- Your constraints go here -->
    <iso:pattern id="RESERVATION">
        <iso:p>
        This pattern validates call activities with RESERVATION operation code.
        </iso:p>
        <iso:rule context="$callActivity[bpmn:extensionElements/activiti:in[(@target='OPERATION_CODE') and (@sourceExpression='RESERVATION')]]/bpmn:extensionElements">
            <iso:assert test="count(($inSourceExpression='RESERVATION') and ($inTarget='OPERATION_CODE')) = 0">err1</iso:assert>
            <iso:assert test="count(($inSourceExpression='true') and ($inTarget='IS_SYNC')) = 1">err2</iso:assert>
        </iso:rule>
    </iso:pattern>
</iso:schema>

这是我的 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) 查看验证结果,但我想查看(报告就足够了)哪些规则失败了(err1err2)。我读过 SVRL 但我不知道如何生成报告。 谢谢。

只需调用 applySchematronValidationToSVRL 即可获取完整的 SVRL(Schematron 验证结果列表)文档。您可以查询失败的断言或报告。

仅打印失败断言的代码示例:

SchematronOutputType schematronOutputType = schematronResourcePure.applySchematronValidationToSVRL(streamSource);
List<Object> 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());
    }
}