7

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?

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
brabster
  • 42,504
  • 27
  • 146
  • 186
  • Either JAXP doesn't support RelaxNG very well, or I must be doing something wrong when I try to use it. – Jason S May 26 '09 at 12:53
  • Haven't tried RelaxNG tbh, just WXS – brabster May 26 '09 at 12:59
  • Not quite a dupe, but see also [Where can I find a Java implementation of an ISO Schematron validator?](http://stackoverflow.com/questions/10126256/where-can-i-find-a-java-implementation-of-an-iso-schematron-validator) – Pops Aug 03 '12 at 22:17

3 Answers3

7

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.

George Bina
  • 1,171
  • 7
  • 7
  • "JAXP is just an API and it does not require support for Relax NG from an implementation." The word "not" in this sentence is a mistake, right? You mean it *does* require support, right? – Avi Flax Oct 09 '09 at 01:49
  • No, it is not a mistake. JAXP requires support only for W3C XML Schema, for other schema languages you need to check to see if the implementation supports that. See also http://java.sun.com/developer/technicalArticles/xml/jaxp1-3/#Validate%20XML%20against%20any%20schema – George Bina Nov 02 '09 at 08:34
3

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.

Philip Helger
  • 1,814
  • 18
  • 28
2

You can check out SchematronAssert (disclosure: my code). It is meant primarily for unit testing, but you may use it for normal code too. It is implemented using XSLT.

Unit testing example:

ValidationOutput result = in(booksDocument)
    .forEvery("book")
    .check("author")
    .validate();
assertThat(result).hasNoErrors();

Standalone validation example:

StreamSource schemaSource = new StreamSource(... your schematron schema ...);
StreamSource xmlSource = new StreamSource(... your xml document ... );
StreamResult output = ... here your SVRL will be saved ... 
// validation 
validator.validate(xmlSource, schemaSource, output);

Work with an object representation of SVRL:

ValidationOutput output = validator.validate(xmlSource, schemaSource);
// look at the output
output.getFailures() ... 
output.getReports() ...
j_maly
  • 1,091
  • 1
  • 13
  • 27