XML technologies / Schematron / Compact and modular programming / Abstract rules
(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 7)
Another modular structure in Schematron is the abstract rule. Unlike concrete rules, abstract rules have no context node; they are general collections of tests. Concrete rules can call or instantiate them.
Thus, one central collection of tests can check different context nodes, as here:
<rule context="arc:tier">
<extends rule="gewicht"/> (1)
<rule>
<rule abstract="true" id="gewicht"> (2)
<report test="parent::*/arc:tier/arc:gewicht &lt; (arc:gewicht div 10)">Noah, das Tier ist zu schwer für seine Zimmergenossen! Es könnte einen zertrampeln.</report>
</rule>(1) Element <extends> references an abstract rule. Attribute rule receives its ID (gewicht). The concrete rule’s context is transferred to the abstract rule.
(2) To create an abstract rule, give the <rule> element an abstract attribute with value true. An abstract rule always needs an id attribute for reference. Here its ID is gewicht .
So far this offers no benefit: the abstract rule is not reused yet. We need another rule referencing the same test. Thus,
<pattern>
<rule context="arc:tier[@fleischfresser='ja']">
<report test="parent::*/arc:tier[@fleischfresser='nein']">Es gibt Fleischfresser und Pflanzenfresser in einer Unterkunft. Die Tiere sind keine Nahrungsquelle!</report>
<report test="parent::*/arc:tier/arc:gewicht < (arc:gewicht div 2)">Noah, dieser Fleischfresser ist zu stark (schwer) für seinen Zimmergenossen. Er könnte ihn als Nahrungsquelle benutzen.</report>
</rule>
[…]
</pattern>becomes
<pattern>
<rule context="arc:tier[@fleischfresser='ja']">
<report test="parent::*/arc:tier[@fleischfresser='nein']">Es gibt Fleischfresser und Pflanzenfresser in einer Unterkunft. Die Tiere sind keine Nahrungsquelle!</report>
<extends rule="gewicht"/> (1)
</rule>
<rule context="arc:tier">
<extends rule="gewicht"/>
</rule>
<rule abstract="true" id="gewicht">
<report test="parent::*/arc:tier/arc:gewicht < (arc:gewicht div 10)">Noah, das Tier ist zu schwer für seine Zimmergenossen! Es könnte einen zertrampeln.</report>
</rule>
</pattern>(1) A concrete rule containing other tests can also include an abstract rule.
Reusing the rule affects its meaning, however. Carnivores should weigh at most twice as much as their roommates, yet this query allows ten times the weight because it copied the herbivore test completely.
Parameterize the abstract rule with variables to handle this. Each calling concrete rule declares a variable of the same name, usable inside the abstract rule:
<pattern>
<rule context="arc:tier[@fleischfresser='ja']">
<let name="faktor" value="2"/> (1)
<report test="parent::*/arc:tier[@fleischfresser='nein']">
Es gibt Fleischfresser und Pflanzenfresser in einer Unterkunft.
Die Tiere sind keine Nahrungsquelle!</report>
<extends rule="gewicht"/>
</rule>
<rule context="arc:tier">
<let name="faktor" value="10"/> (1)
<extends rule="gewicht"/>
</rule>
<rule abstract="true" id="gewicht">
<report test="parent::*/arc:tier/arc:gewicht < (arc:gewicht div $faktor)"> (2)
Noah, das Tier ist zu schwer für seine Zimmergenossen! Es könnte
einen zertrampeln bzw. als Nahrungsquelle benutzen. Dieses Tier
darf maximal <value-of select="$faktor"/>-mal so schwer sein, wie
der schwächste Zimmergenosse.</report> (3)
</rule>
</pattern>(1) In the carnivore rule, variable $faktor takes value 2. In the next rule, the same variable takes 10. Every calling rule must declare a variable named faktor; otherwise the abstract rule cannot use it.
(2) The original versions used the following XPath query: parent::*/arc:tier/arc:gewicht < (arc:gewicht div X). At the position marked X, the two rules differ: the first uses value 10 and the second value 2.
(3) Different rules now produce the same message, so make it more general. A variable can help.
Copyright © dpunkt.verlag GmbH 2011
You may print the online version for private use. Otherwise, this chapter from "Schematron - Effiziente Business Rules für XML-Dokumente" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, phone 06221-14830, fax 06221-148399, hallo(at)dpunkt.de
back to top