6

The ISO Schematron standard has been out for two years now, but I'm still unable to find a Java implementation using ISO Schematron XSLT files (as opposed to files from an older version of Schematron, e.g. here: http://uploading.com/files/c9c9cb87/SchematronXpath.jar/).

Does anyone know of a production-ready ISO schema validator that can be easily called from a Java method?

Pops
  • 30,199
  • 37
  • 136
  • 151
Chris
  • 4,212
  • 5
  • 37
  • 52
  • 1
    Not quite a dupe, but see also [How can I validate documents against Schematron schemas in Java?](http://stackoverflow.com/q/910476/122607) – Pops Aug 03 '12 at 22:17

4 Answers4

9

Additionally you may use ph-schematron which provides both support for conversion to XSLT as well as a native plain Java validation, which is quicker than the XSLT version in nearly all cases. See https://github.com/phax/ph-schematron/ for the details as well as a quick intro. Example code to check if an XML file matches a Schematron file:

public static boolean validateXMLViaPureSchematron (File aSchematronFile, File aXMLFile) throws Exception { 
  final ISchematronResource aResPure = SchematronResourcePure.fromFile (aSchematronFile);
  if (!aResPure.isValidSchematron ()) 
    throw new IllegalArgumentException ("Invalid Schematron!"); 
  return aResPure.getSchematronValidity(new StreamSource(aXMLFile)).isValid ();
}
Philip Helger
  • 1,814
  • 18
  • 28
  • Hello Philip, using ph-schematron is a good idea but it needs to be supported by how-to's. I couldn't find a well-rounded tutorial about it. – GokcenG Dec 24 '15 at 12:06
  • Thanks for the info. It would be great if you could file issues on the GitHub project :) – Philip Helger Dec 30 '15 at 09:38
  • found ph-schematron to be quite useful and easy to use. I might write a tutorial on how I am using it. Thanks Philip, for making the library available. – MohamedSanaulla Dec 31 '15 at 06:59
  • There is a basic tutorial available at http://phax.github.io/ph-schematron/ - any extensions are welcome – Philip Helger Jan 03 '16 at 12:37
  • @PhilipHelger .. thanks for the implementation and tutorial. But I am still confused how to run ? Can you please post a step by step and simple tutorial about this? – Sunil Garg Dec 27 '16 at 07:08
  • @SunilGarg Okay, will do. Would be great if you could issue an issue on GitHub – Philip Helger Dec 27 '16 at 13:14
6

Probatron4j can validate against ISO Schematron. The website provides a single, self-contained JAR that's designed to be run from the command line, but it's easy to call Probatron from a Java method if you have its source code. Here's a simplified version of how I did it:

public boolean validateSchematron(InputStream xmlDoc, File schematronSchema) {
    // Session = org.probatron.Session; think of it as the Main class
    Session theSession = new Session();
    theSession.setSchemaSysId(schematronSchema.getName());
    theSession.setFsContextDir(schematronSchema.getAbsolutePath());

    // ValidationReport = org.probatron.ValidationReport; the output class
    ValidationReport validationReport = null;
    try
    {
        validationReport = theSession.doValidation(xmlDoc);
    }
    catch(Exception e) { /* ignoring to keep this answer short */ }

    if (validationReport == null ||
        !validationReport.documentPassedValidation()) {
        return false;
    }
    return true;
}

You'll need to make a few minor modifications to let Probatron know it's not being run from within a JAR file, but it doesn't take long.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • `theSession.doValidation(xmlDoc);` does not take an `InputStream`, only a `String` – btiernay Mar 31 '15 at 14:52
  • 2
    Be aware that Probatron is licensed under Affero GPL. IANAL, but you may want to consult one before making use of Probatron in commercial environments. – Mattias Jiderhamn Sep 17 '15 at 14:19
0

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
-1

The jing library works for me.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • 3
    Although jing does work with Schematron 1.5, it does not yet support the ISO version of Schematron. See [issue 23](http://code.google.com/p/jing-trang/issues/detail?id=23&colspec=ID%20Type%20Status%20Priority%20Stars%20Module%20Summary). – Pops Aug 03 '12 at 22:15
  • Update: Jing was moved to [this Github repo](https://github.com/relaxng/jing-trang/issues/23). – wearego Apr 17 '19 at 11:50