phax / ph-schematron

Java Schematron library that supports XSLT and native application
Apache License 2.0
110 stars 36 forks source link

Error parsing the following SVRL #124

Closed robertmarkbram closed 2 years ago

robertmarkbram commented 2 years ago

I am applying a Schematron rule set to an XML document and having trouble figuring out how to deal with the SVRL. I have:

import com.helger.schematron.ISchematronResource;
import com.helger.schematron.SchematronHelper;
import com.helger.schematron.svrl.jaxb.SchematronOutputType;
import com.helger.schematron.xslt.SchematronResourceXSLT;
import javax.xml.transform.stream.StreamSource;
import java.io.File;

// ...

    void xsltValidation() {
        final String schematronPath = "/schematron/SSCCT/Validation/SSCCT Validation RulesAll.xsl";
        final ISchematronResource schematronResource = SchematronResourceXSLT.fromClassPath(schematronPath);
        log.info("Loaded the schematron file: {}", schematronPath);
        assertTrue(schematronResource.isValidSchematron());
        log.info("Schematron file is valid.");

        final String xbrlPath = "src/test/resources/validate/xbrl-schematron/xbrl-schematron_002.xml";
        log.info("Now applying it to xbrl: {}", xbrlPath);
        final SchematronOutputType schematronOutputType =
                SchematronHelper.applySchematron(schematronResource, new StreamSource(new File(xbrlPath)));
        //        final Document document = schematronResource.applySchematronValidation(new StreamSource(xbrlStream));
    }
}

I see "Schematron file is valid." and then "Now applying it to xbrl ..." so I know it validated. But on the applySchematron I am getting:

2021-12-16 22:13:23.457 DEBUG   --- [           main] c.h.commons.lang.ServiceLoaderHelper     : Finished retrieving all 2 SPI implementations of interface com.helger.commons.hashcode.IHashCodeImplementationRegistrarSPI
2021-12-16 22:13:23.461 DEBUG   --- [           main] c.h.c.h.HashCodeImplementationRegistry   : Reinitialized com.helger.commons.hashcode.HashCodeImplementationRegistry
2021-12-16 22:13:24.071 ERROR   --- [           main] .s.x.AbstractSchematronXSLTBasedResource : Error parsing the following SVRL:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Event xmlns="http://sbr.gov.au/comn/event.02.data">
  <MaximumSeverity.Code>Error</MaximumSeverity.Code>
  <EventItems>
    <EventItem>
 ...
  </EventItems>
</Event>

javax.xml.bind.UnmarshalException: null
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:310)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:548)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:350)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:318)
    at com.helger.jaxb.IJAXBReader.lambda$read$1(IJAXBReader.java:343)
    at com.helger.jaxb.GenericJAXBMarshaller.read(GenericJAXBMarshaller.java:504)
...
Caused by: org.xml.sax.SAXParseException: cvc-elt.1.a: Cannot find the declaration of element 'tns:Event'.
    at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204)

My questions:

  1. I understand that the SVRL seems to be in a domain specific (XBRL) but I am not sure how to deal with this using ph-schematron.
  2. I didn't quite understand what the difference is between using SchematronHelper.applySchematron vs schematronResource.applySchematronValidation.

Thanks,

Rob :)

phax commented 2 years ago

Hi Rob, How does Schematron effectively work:

  1. You have a set of predefined XSLTs (from ISO Schematron or SchXslt) that convert your source Schematron to an XSLT
  2. That XSLT from step 1 is than applied onto your XML for validation and it creates and SVRL (Schematron Validation Report Language)
  3. That SVRL needs to be parsed for all the failed assertions / reports

In your scenario the file /schematron/SSCCT/Validation/SSCCT Validation RulesAll.xsl seems to be the result of step 1, but it doesn't seem to create SVRL as expected in step 2, but some other "report language". Unfortunately that is not too uncommon (also done like this in OIOUBL), that's why there is the method ISchematronResource.applySchematronValidation that returns the DOM Document only, and the caller is responsible for interpreting the result.

The SchematronHelper methods are only there to convert a checked Exception into an unchecked Exception. They are legacy now, as the ISchematronResource was pimped accordingly. I will deprecate these two methods for the next release.

robertmarkbram commented 2 years ago

Thanks - that's what I need. I will work with the document to get my output.

final Document document = schematronResource.applySchematronValidation(new StreamSource(new File(xbrlPath)));

Rob :)