0

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.

tsoiland
  • 101
  • 7
  • The RELAX NG schema for Schematron is included in the ISO standard. There is an unofficial copy at https://github.com/Schematron/schema. I expect you would need to use `trang` to generate an XSD version for use with JAXB. However, to run the Schematron would require a Schematron processor to (usually) transform the Schematron into XSLT that is run on the tested document. – Tony Graham Jun 19 '22 at 15:34

2 Answers2

0

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.

klakegg
  • 181
  • 1
  • 8
0

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