ph-schematron validation error message
Asked Answered
C

1

6

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.

Constancy answered 24/12, 2015 at 14:12 Comment(0)
L
7

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());
    }
}
Lovesick answered 7/1, 2016 at 8:53 Comment(3)
Alternatively you can use SVRLHelper.getAllFailedAssertions (schematronOutputType) - that returns a list of SVRLFailedAssert instead.Lovesick
How to get the source XML line number of a FailedAssert?Meraz
@RobertoRusso that is unfortunately not possible. To figure out what element it is, you need to dig deeper into the created SVRL. Check the active-pattern and the fired-rules elements to get more context. I suggest you get yourself the SVRL printed as a whole (new SVRLMarshaller ().getAsString (schematronOutputType)) to understand. hthLovesick

© 2022 - 2024 — McMap. All rights reserved.