4
votes

I'm using ph-schematron to validate my XML files. I'm able to validate the files correctly but I couldn't find how to generate reports about failing assertions.

This is my context(point-of-interest):

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

This is my Schematron schema:

<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>

This is my Java code:

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();
}

I can see the result of the validation by calling schematronResourcePure.getSchematronValidity(streamSource) but I want to see (a report would be sufficient) which rules are failed(err1 or err2). I've read about SVRL but I don't know how to generate report. Thank you.

1

1 Answers

6
votes

Simply call applySchematronValidationToSVRL to get the full SVRL (Schematron Validation Result List) document. You can query it for failed asserts or reports.

Code example where only failed asserts are printed:

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());
    }
}