Category Archives: Validation

Schematron support

Mint2 now supports validation using Schematron rules. Schematron rules are related to an XmlSchema instance and can be provided to the system through appinfo schema annotations, using external schematron files, or both.

Define schematron rules

You can define schematron rules inside an XSD by adding anĀ appinfo annotation like this (example from ORE.xsd):

<element name="Aggregation">
      <documentation>Aggregated CHO: a link to the original CHO plus a new provider</documentation>
      <appinfo xmlns:sch="">
        <sch:pattern name="Either Is shownby or is shownat should be present" >
          <sch:rule context="ore:Aggregation">
            <sch:assert test="edm:isShownAt or edm:isShownBy">A ore:Aggregation must have either edm:isShownAt or edm:isShownBy</sch:assert>

Alternatively, you can provide schematron rules using external schematron files. If the schematron schema configuration property is defined, the system will first try to locate this file in the same directory the XSD file exists. Otherwise it will try to locate a file with the same name as the XSD file with a .sch suffix (i.e. EDM-INTERNAL.xsd.sch). The system will also try to merge rules defined from xsd and external files.

Schematron validation

Schematron validation is available by using the class. This class provides variants of the validateSchematron(Source source, XmlSchema schema, ReportErrorHandler handler) method to validate source using an XSL generated by the schematron rules related to the schema XmlSchema. If a handler ReportErrorHandler is provided, errors will be added in the handler’s schematronErrors JSONArray:

Schematron XSL generation

The schematron XSL used to validate an xml is generated by the class by using the getXSL method.
This requires a string that contains a complete schematron document. Rules from XSD appinfo annotations can be wrapped in a complete schematron document by using the wrapRules method and using the schema’s namespace declarations (from schema’s configuration) to include in the document:

XmlSchema schema = <an XmlSchema instance>
String schematronRules = schema.getSchematronRules();

SchematronXSLTProducer schematron = SchematronXSLTProducer.getInstance();
String wrapped = schematron.wrapRules(schematronRules, schema.getConfigurationNamespaces());
String xsl = schematron.getXSL(wrapped);