XML technologies / Schematron / User-friendly error messages / Generating notices dynamically
(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 6)
Even specialists often struggle to relate generic parser-generated messages to a specific error. Such messages are broad because the same error can have different causes. Schematron can generate messages tailored to context.
Element <value-of> can add instance content to error messages. A simple example illustrates this:
<rule context="arc:tier"> (1)
<report test="count(//arc:tier[arc:art=current()/arc:art]) > 2">In der Arche gibt es mehr als zwei Tiere der Art <value-of select="arc:art"/>.</report> (2)
</rule> (1) A rule from the ark schema checks whether more than two animals of one species share accommodation.
(2) Instead of the generic message “In dieser Unterkunft gibt es mehr als zwei Tiere dieser Art”, the rule now adds element <value-of>. The select attribute specifies an XPath expression selecting the content. As elsewhere in rules, relative XPath starts at the context node. Thus child element <art> is output.
Output:
In dieser Unterkunft gibt es mehr als 2 Tiere der Art Zebra.
In dieser Unterkunft gibt es mehr als 2 Tiere der Art Löwe.
This simple addition makes messages more recognizable and appealing. But element <value-of> offers much more, as the next example shows:
<rule context="arc:nutzlast"> (1)
<report test="number(//arc:nutzlast) < sum(//arc:gewicht)">Noah, du hast zu viele Tiere an Bord. Die Ladung von <value-of select="sum(//arc:gewicht)"/> Kilogramm überschreitet die Nutzlast deiner Arche.</report> (2)
</rule> (1) This familiar rule checks whether the total animal weight exceeds the payload.
(2) Element <value-of> examines all animals and outputs their total weight.
Output:
Noah, du hast zu viele Tiere an Bord. Die Ladung von 11187 Kilogramm überschreitet die Nutzlast deiner Arche.
A message can of course contain any number of <value-of> elements:
<rule context="arc:nutzlast">
<report test="number(//arc:nutzlast) < sum(//arc:gewicht)">Noah, du hast zu viele Tiere an Bord. Die Ladung von <value-of select="sum(//arc:gewicht)"/> kg überschreitet die Nutzlast deiner Arche von <value-of select="//arc:nutzlast"/> kg. Reduziere deine Last um <value-of select="sum(//arc:gewicht) - number(//arc:nutzlast)"/> kg oder baue deine Arche aus.</report>
</rule> Output:
Noah, du hast zu viele Tiere an Bord. Deine Ladung von 11187 kg überschreitet die Nutzlast deiner Arche von 10000 kg. Reduziere deine Last um 1187 kg oder baue deine Arche aus.
There are limits, however, for example grammatical rules. Trying to generate this message is unwise:
Noah, der Elefant ist zu schwer für seine Zimmergenossen! Er könnte einen zertrampeln.
If the overweight animal is a giraffe, the German message would instead have to begin “Noah, die Giraffe ist zu schwer”. The different German article makes this construction nearly impossible.
Note for XPath experts:
The only option would be metadata in the instance identifying the species’ grammatical gender, followed by an XPath2 query with if-then-else. Such metadata is rare and should never be added solely to optimize a Schematron schema.
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