The business rule principle

The origin of the business rule principle does not lie in Schematron; a Schematron schema is only suitable for the technical formulation of business rules. In this way, XML documents can be checked on the basis of these business rules. A business rule is each possible condition which can be imposed on a XML document. For instance, a formulated business rule for the Ark instance would be as follows:

  • Carnivorous animals must not be accommodated with herbivorous animals in the same room.

Schematron uses rules (<rule> elements) which can also combine several business rules. In some cases various Schematron rules may be required in order to describe a single business rule. Depending on a context, a Schematron rule describes certain criteria on which the document is to be checked.

With regard to the determination of the context node, it does not necessarily have to be a single node; it can also include a node set (e.g. all elements with a certain name). The criteria to be checked are defined by so-called "tests" which are assigned to a rule. If a test fails, a predetermined error message will be outputted. Therefore, the context nodes of the abovementioned rule would be all carnivorous animals of the Ark instance. The criterion to be queried would be that these animals must not be accommodated with herbivorous animals in the same room. The following variants of the business rule shall demonstrate that Schematron rules with different context nodes can also describe the same business rule:

  • Herbivorous animals must not be accommodated with carnivorous animals in the same room.
  • In one room either only herbivorous or only carnivorous animals may be accommodated.

Now, the respective rules would have another context node, namely herbivorous animals or rooms. As a consequence, the appropriate tests have to be formulated differently.

 

How to define a business rule?
This was the theory. The implementation of business rules in Schematron is not too difficult anymore. The <rule> element contains the rule; the context attribute determines the context node. Furthermore, the <rule> element contains the tests and conditions which shall be assigned to the rule.

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
   <ns uri="http://www.schematron.info/ark" prefix="ark"/>
   <pattern>
      <rule context="ark:animal[@carnivore='yes']">                 (1)
         <report test="parent::*/ark:animal[@carnivore='no']">
          There are carnivores and herbivores in one accommodation.
          The animals are not a food source!
         </report>
      </rule>
   </pattern>
</schema>

(1) The rule is applied to all <animal> elements of the instance whose carnivore attributes have the value yes. The application of a rule to an element means that all tests defined in this rule will be checked for the element.

Note for XSLT experts:

The business rule principle corresponds to the principle of the XSLT template with the match attribute. When transforming Schematron into XSLT, all rules are internally implemented in templates and the appropriate values of the context attributes are adopted as match attributes. The transformation of the said example is as follows:

<xsl:template match="ark:animal[@carnivore='yes']">
[…]
<!-- The tests are implemented as xsl:if elements -->
[…]
</xsl:template>

 

By using a XPath expression, the selection of the context node may also contain conditions in the form of predicates. The example above checks whether a carnivore is accommodated together with a herbivore in the same room. This condition could be extended by using additional predicates. You can check, for example, whether a male carnivore and a herbivore are living together in the same room (under the — political and biological incorrect — assumption that a male animal is the stronger one).

 

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <ns uri="http://www.schematron.info/ark" prefix="ark"/>
  <pattern>
    <rule context="ark:animal[@carnivore='yes'] [@sex='male']">   (1)
      <report test="parent::*/ark:animal[@carnivore='no']">
        There are carnivores and herbivores in one accommodation.                    
        The animals are not a food source!
      </report>
    </rule>
  </pattern>
</schema>
(1) In this example, the XPath expression in the context attribute was supplemented by a further predicate. Now, the rule is only applied to animals which are carnivorous and male. That means that a test has only to check whether a herbivorous animal is living in the same room. This kind of preselection is not a necessity. The same criteria could also be queried during a test. However, since the context attribute determines to which nodes the rule shall be applied, a preselection can significantly minimise the number of the rules to be applied. This has a positive effect on the performance of the testing process. In addition, the XPath expressions of the next tests are simplified.

   

<< back next >>

 

 

 


Copyright © dpunkt.verlag GmbH 2011
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "Schematron - Effiziente Business Rules für XML-Dokumente" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo(at)dpunkt.de