Monday, January 23, 2017

Schematron

A language for making assertions about the presence or absence of patterns in XML documents. 


Use of Schematron

  • Busines rule validation
  • Data reporting and general validation
  • Quality control and constraints checking
  • Naming and design rule validation

How it works?


1) Find useful context nodes in the document based on XPath

2) Check to see of some other XPath expressions are true for each of those contexts

3) The report which asserts has failed.


Main Elements


schematron components


1) Schema: Schema of the Schematron


<?xml version="1.0" encoding="utf-8"?>}}
<sch:schema xmlns="http://purl.oclc.org/dsdl/schematron"
      xmlns:sch="http://purl.oclc.org/dsdl/schematron"
      xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
      xmlns:ef="http://www.efatura.gov.tr/envelope-namespace">

2) ns (namespace): Element to provide namespace and prefix


<sch:ns uri="your_schema_url" prefix="art" />

3) Pattern : section under different rules for a context are defined


<sch:pattern name="Only two paragraph tag">
  <sch:rule context="art:html/art:body/art:div[1]">
     <sch:assert test="count(//q:p) &lt;=2">Only two p tags</sch:assert>
  </sch:rule>
 </sch:pattern>

4) Rule : context is defined under which assertions are verified


<sch:rule context="art:html/art:body/art:div[1]">
   <sch:assert test="count(//q:p) &lt;=2">Only two p tags</sch:assert>
</sch:rule>

5) Assert: show failed asserts


<sch:assert test="count(//q:p) &lt;=2">Only two p tags</sch:assert>

6) Report : show passed rules


<sch:report test="not(count(//q:p) &lt;=2)">Only two p tags</sch:report>

Schematron Implementation


There are many implementations for Schematron. Probatron is one of these and below is the link to download the jar file or source code

https://code.google.com/archive/p/schematron/downloads


how schematron implementation works

Example


Following example will use Probatron.jar to verify business rule of XML.

Xml to test : candidate_doc.xml


<artist xmlns="your_schema_url" id="123">
    <body class="clear">
       <div>
          <p>
            <t>Adele -Pop Singer</t>
          </p>
          <p>
            <t>Hello</t>
          </p>
          <p>
            <t>Set fire to the rain</t>
          </p>
        </div>
    </body>
</artist>


Schematrn doc : schemaron_validation_doc.sch



<?
xml version="1.0" encoding="utf-8"?>
<sch:schema xmlns="http://purl.oclc.org/dsdl/schematron"
      xmlns:sch="http://purl.oclc.org/dsdl/schematron"
      xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
      xmlns:ef="http://www.efatura.gov.tr/envelope-namespace">

  <sch:ns uri="your_schema_url" prefix="art" />

  <sch:pattern name="Only two paragraph tag">
     <sch:rule context="art:html/art:body/art:div[1]">
        <sch:assert test="count(//q:p) &lt;=2">Only two p tags</sch:assert>
     </sch:rule>
  </sch:pattern>
</sch:schema>


Java Code: Driver.java


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

import org.probatron.Session;
import org.probatron.ValidationReport;

public class Driver {
 static String fixArg(String arg) {
  // user concession, if no URL scheme assume these are files
  return arg.indexOf(":") == -1 ? "file:" + arg : arg;
 }

 public static void main(String[] args) {
  Session theSession = new Session();
  OutputStream os=null;
  try {
   os = new FileOutputStream("report.xml");
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  }
  String candidate = fixArg( "candidate_doc.xml" );
  theSession.setSchemaDoc( fixArg("schematron_validation_doc.sch" ));
  try {
   ValidationReport vr = theSession.doValidation(candidate);
   byte[] data = vr.reportAsBytes();
   String strData = new String(data);
   vr.streamOut(os); // to print in file
   vr.sreamOut(System.out); // to print on console
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
  System.out.println("Done");
 }
}


Output : report.xml


<?xml version="1.0" standalone="yes"?>

<svrl:schematron-output title="" schemaVersion=""      xmlns:svrl="http://purl.oclc.org/dsdl/svrl">
<svrl:ns-prefix-in-attribute-values prefix="q" uri="your_schema_url">
</svrl:ns-prefix-in-attribute-values>

  <svrl:failed-assert test="count(//p) &lt;=2"         location="/html[1]/body[1]/div[1]" line="4" col="22">
      <svrl:text>Only two p tags</svrl:text>
  </svrl:failed-assert>
</svrl:schematron-output>

References:


Official WebSite:

No comments:

Post a Comment