XML technologies / Schematron / Compact and modular programming / Abstract patterns
(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 7)
Besides abstract rules, Schematron supports abstract patterns. An abstract pattern can be called or instantiated by another pattern, with a parameter list. Our ark schema provides another example:
<rule context="arc:tier[@geschlecht='männlich']">
<report test="number(arc:alter) > number(//arc:maxReproduktionsalter/arc:tier_art[arc:name=current()/arc:art]/arc:männlich)">Das Männchen ist zu alt, Noah! Es wird sich nicht mehr fortpflanzen können. Sorge für die natürliche Auslese.</report>
</rule>
<rule context="arc:tier[@geschlecht='weiblich']">
<report test="number(arc:alter) > number(//arc:maxReproduktionsalter/arc:tier_art[arc:name=current()/arc:art]/arc:weiblich)">Das Weibchen ist zu alt, Noah! Es wird sich nicht mehr fortpflanzen können. Sorge für die natürliche Auslese.</report>
</rule>
The rules differ only at the highlighted points. Since the two XPath tests access different elements, variables are not an easy solution. The local-name() function is one possibility, but an abstract pattern is more elegant. First create one without calling it yet.
<pattern abstract="true" id="geschlecht"> (1)
<rule context="arc:tier[@geschlecht='$geschlecht']"> (2)
<report test="number(arc:alter) > number(//arc:maxReproduktionsalter/arc:tier_art[arc:name=current()/arc:art]/arc:$geschlecht)">Das Tier ist zu alt, Noah! Es wird sich nicht mehr fortpflanzen können. Sorge für die natürliche Auslese.</report> (3)
</rule>
</pattern>
(1) Add an abstract attribute with value true to the pattern. As with abstract rules, it now requires an id attribute.
(2) Wherever the rules in männlich/weiblich differ, reference a parameter, $geschlecht, not yet defined. Somewhat confusingly, parameters are referenced in the same way as variables.
(3) Since we cannot distinguish Männchen and Weibchen, use the neutral Tier.
The difference from variables is clearest in attribute test. XPath does not treat the parameter as a variable. Before resolving the XPath expression, search and replace substitutes every string $geschlecht with the parameter value (weiblich or männlich). Only then is XPath syntax checked.
This abstract pattern is not yet used and has no effect until called. Call it through another <pattern> element. Attribute is-a references it, and a parameter list is supplied. Each parameter needs a <param> element containing its name (attribute name) and value (attribute value). A <pattern> element calling another pattern may contain only <param> elements.
<pattern is-a="geschlecht"> (1)
<param name="geschlecht" value="männlich"/> (2)
</pattern>
<pattern is-a="geschlecht"> (1)
<param name="geschlecht" value="weiblich"/> (2)
</pattern>
<pattern abstract="true" id="geschlecht">
<rule context="arc:tier[@geschlecht='$geschlecht']"> (3)
<report test="number(arc:alter) > number(//arc:maxReproduktionsalter/arc:tier_art [arc:name=current()/arc:art]/arc:$geschlecht)">Das Tier ist zu alt, Noah! Es wird sich nicht mehr fortpflanzen können. Sorge für die natürliche Auslese.</report>
</rule>
</pattern>
(1) The <pattern> elements each call the abstract pattern through attribute is-a, whose value is its ID (geschlecht). It is called twice, for male and female animals.
(2) Our pattern uses $geschlecht as a parameter, so a <param> element is created per concrete pattern. Unlike <xsl:param> in XSLT, attribute value does not evaluate XPath to find the parameter value: the attribute string itself is the value. Strings therefore lack the single quotes, such as 'weiblich', that XPath expressions would require.
(3) Parameter reference $geschlecht is now completely replaced by its assigned string. After substitution, XPath expressions must be syntactically correct.
Parameters work only in XPath expressions. To influence error text, insert parameter-dependent text through a <value-of> element. XPath strings require single quotes. Instead of Das Tier ist zu alt…, we now want strings Das männliche Tier ist zu alt… or Das weibliche Tier ist zu alt.... Generate the appropriate <value-of> elements through parameters.
<pattern is-a="geschlecht">
<param name="geschlecht" value="männlich"/>
</pattern>
<pattern is-a="geschlecht">
<param name="geschlecht" value="weiblich"/>
</pattern>
<pattern abstract="true" id="geschlecht">
<rule context="arc:tier[@geschlecht='$geschlecht']">
<report test="number(arc:alter) > number(//arc:maxReproduktionsalter/arc:tier_art[arc:name=current()/arc:art]/arc:$geschlecht)">Das <value-of select="'$geschlechte'"/> Tier ist zu alt, Noah! Es wird sich nicht mehr fortpflanzen können. Sorge für die natürliche Auslese.</report> (1)
</rule>
</pattern>
(1) Notice that the parameter reference appears to be named $geschlechte. The assigned parameter values were weiblich and männlich, but we want the <value-of> element to produce strings männliche and weibliche . Search-and-replace substitution turns the values of attribute select into 'männliche' or 'weibliche', since only string $geschlecht is replaced by the parameter value.
This combines patterns differing only at specific points into one abstract pattern.
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