How can I compile Schematron into Jaxb annotated Java classes?

279 Views Asked by At

I have been using xjc to compile XML Schema into annotated java classes, so that I can generate xml files using jaxb.

How can I do the same for Schematron?

UPDATE: To be more specific: The java classes don't need to validate all the schematron rules, only the parts that pertain to schema, i.e. the parts of the Schematron that could have been expressed as XSD.

This is the specific schema I want to compile: https://github.com/OpenPEPPOL/peppol-bis-invoice-3/blob/master/rules/sch/PEPPOL-EN16931-UBL.sch

The purpose of this is to generate PEPPOL BIS Billing 3.0 invoices and credit notes. Other comments also welcome.

2

There are 2 best solutions below

0
On

Schematron is not a schema definition as is the case with XSDs when using XJC. It is not possible to generate Java classes from Schematron using XJC.

0
On

XSD is a grammar that is able to validate a XML structure. Schematron are rules that helps (not exclusive) to validate business rules on data, not on structure.

If I made a parallel with JAXB generated classes, XSD is able to validate that each XML node fits into a class attribute ; Schematron validates that invoice.vat < invoice.totalAmount. So Schematron rules are usually added to a structural grammar (XSD, RelaxNG, DTD). You'd be able to define some validate() method on your JAXB classes that matches your schematron rules.

I remember a project (15 years ago) that included some Schematron rules into an Xsd grammar, and help to generate validation methods from this. But generated code wasn't very precise, and most of XPath expressions couldn't be transformed into java.

That was based on https://github.com/NIEM/Schematron-in-XSD-Spec but I can not found it.

If I had to implement this, I'll have a look at https://phax.github.io/ph-schematron/ and I'll generate a proxy around JAXB classes that will validate against Schematron rules.

Best regards, Christophe