Schematron

 

§  Schematron is useful whenever you wish to apply and check against rules for the contents of XML documents. Used for business rule validation.

§  A Schematron schema is made up of elements in the  http://www.ascc.net/xml/schematron namespace.

§   Root element has a local name of schema. Schema element should have a descriptive title element.

§  An XML document to be validated with the Schematron rules is a candidate instance or document.

§  Each rule contains a set of individual checks represented using assert or report elements. Rules are organized using pattern elements that contain collections of related rule elements. Rules and patterns can also have descriptive title elements.

§  A Schematron processor operates by examining in document order each node in the candidate instance. For each element, it checks all the patterns, and executes rules that are appropriate for that element. It executes no more than one rule in each pattern.

§   Each rule has a context attribute that determines which elements will trigger that rule.

§  When a rule is fired, the Schematron processor checks for assertions and  reports declared in the body of the rule. An assertion is an XPath expression that you expect to evaluate to true using the rule's context in a valid document. An assert element has a test attribute with the XPath expression; the body of the element is a brief message that expresses the condition that is expected to be true.  assert test allows you to express validation constraints.

§   report is processed very similarly to assert except that the report message is triggered when the boolean value of the test attribute is true, rather than false.  . report instruction that is intended to allow you to trigger more general reporting tasks.

§  Schematron is used for various purposes, some are as follows :-

·         Validating the presence of elements

·         Validating that elements are where expected

·         Validating relative positioning of elements

·         Validating a sequence of elements

·         Validating for a certain number of elements

·         Validating presence and value of attributes

·         Simple validation of element content

·         Validating exclusivity of elements

 

§  Sample program to show the power of  Schematron.:-

 

            Below is sample XML file need to validated

__________________________________________________ 

 

book.xml

 

<?xml version="1.0"?>
<catalog>
   <book id="bok101">
      <author>Gambardella, Matthew</author>
      <title>SCJP</title>
      <genre>Computer</genre>
      <price>495</price>
      <publish_date>2010-10-01</publish_date>
      <description>Sun certified Java programming reference .</description>
   </book>
   <book id="bok102">
      <author>Ralls, Kim</author>
      <title>XML Reference</title>
      <genre>Fantasy</genre>
      <price>595</price>
      <publish_date>2008-12-16</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
</catalog>
 ___________________________________________________
Sample  Schematron for book.xml
 ___________________________________________________

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.ascc.net/xml/schematron">

<title>Schematron for Book catalog</title>

<pattern name="Document root and presence of elements ">

<rule context="/">

<assert test="catalog">Root element should be "doc".</assert>

</rule>

<assert test="book">

The "doc" element must have a "prologue" child.

</assert>

</pattern>

<pattern name="relative position of element">

<rule context="/book/author">

<assert test="not(preceding-sibling::title)">

No "title" may occur before the "author".

</assert>

</rule>

</pattern>

 <pattern name="Minimum number of books in catalog">

<rule context="catalog">

<assert test="count(book) > 1">

At least two books are required in catalog.

</assert>

</rule>

</pattern>

<pattern name="Author attributes">

<rule context="/book">

<assert test="@id">

book must have id attribute.

</assert>

</rule>

</pattern>

</schema> 

 ___________________________________________________



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)