Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate documents against Schematron schemas in Java?

As far as I can tell, JAXP by default supports W3C XML Schema and RelaxNG from Java 6.

I can see a few APIs, mostly experimental or incomplete, on the schematron.com links page.

Is there an approach on validating schematron in Java that's complete, efficient and can be used with the JAXP API?

like image 899
brabster Avatar asked May 26 '09 12:05

brabster


People also ask

How do I validate a schema file?

By default, XML Schema files are validated as you type. To change this, open the Preferences dialog box (Options > Preferences), go to Editor > Document Checking, and deselect the Enable automatic validation option.

Can we validate XML documents against a schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

What is a schematron file?

Schematron is a simple and powerful Structural Schema Language for making assertions about patterns found in XML documents. It relies almost entirely on XPath query patterns for defining rules and checks. Schematron validation rules allow you to specify a meaningful error message.


2 Answers

Jing supports pre-ISO Schematron validation (note that Jing's implementation is based also on XSLT).

There are also XSLT implementations that can be very easily invoked from Java. You need to decide what version of Schematron you are interested in and then get the corresponding stylesheet - all of them should be available from schematron.com. The process is very simple simple, involving basically 2 steps:

  • apply the skeleton XSLT on your Schematron schema to get a new XSLT stylesheet that represents your Schematron schema in XSLT
  • apply the obtained XSLT on your instance document or documents to validate them

JAXP is just an API and it does not require support for Relax NG from an implementation. You need to check the specific implementation that you use to see if that supports Relax NG or not.

like image 120
George Bina Avatar answered Sep 21 '22 17:09

George Bina


A pure Java Schematron implementation is located at https://github.com/phax/ph-schematron/ It brings support for both the XSLT approach and the pure Java approach.

like image 28
Philip Helger Avatar answered Sep 20 '22 17:09

Philip Helger