XML technologies / Schematron / User-friendly error messages / Error hierarchy

(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 6)

In larger Schematron schemas, rules and potential errors can become numerous. Large instances with many errors quickly make reports unwieldy. Equal-priority errors can simply be fixed in order, but Schematron also supports warnings or notices that need attention only in certain circumstances. Organize the schema’s errors into a hierarchy for this purpose.

Defining an error hierarchy

Attribute role on <assert>, <report> or <rule> assigns test priorities through keywords. Neither keywords nor their effects are standardized; the implementation defines them, so examples cannot be universal. We use oXygen, which supports this attribute from 11.1.

It provides these keywords:

Keyword

Error category

fatal

Critical error

error

Error

warn

Warning

warning

info

Information

information

Table: oXygen keywords for error messages

oXygen displays and handles messages according to priority. Errors are underlined in red, warnings in yellow, while information appears only in an informational report, not the editing window.

We can classify the ark schema’s rules and tests into these categories. First categorize the tests.

Error category

Test meaning

Critical error

Compliance with payload capacity

Error

Compliance with maximum reproductive age

Separation of herbivores and carnivores

Fewer than two animals of one species

Warning

More than two animals of one species

Information

More than six animals in one room

Table: error categories for the ark example

An overloaded ark risks capsizing and endangering all future life on earth: a critical error. Threats to one species are ordinary errors. More than two animals of a species does not threaten its survival, so it has lower priority. Rooms with more than six animals merit a recommendation, merely additional information.

Implement this classification in Schematron as follows:

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <ns uri="http://www.schematron.info/arche" prefix="arc"/>
  <pattern>
    <rule context="arc:tier[@geschlecht='männlich']">
      <report role="error" test="number(arc:alter) &gt; 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>   (1)
    </rule>
  </pattern>
  <pattern>
    <rule context="arc:nutzlast">
      <report role="fatal" test="number(//arc:nutzlast) &lt; sum(// arc:gewicht)">Noah, du hast zu viele Tiere an Bord. Die Ladung überschreitet die Nutzlast deiner Arche.</report>   (1)
    </rule>
  </pattern>
  <pattern>
    <rule role="error" context="arc:tier[@fleischfresser='ja']">   (2)
      <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 &lt; (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>
  <pattern>
    <rule context="arc:tier">
      <report role="warn" test="count(//arc:tier[arc:art=current()/arc:art]) &gt; 2">In der Arche gibt es mehr als zwei Tiere dieser Art.</report>   (1)
      <report test="count(parent::*/arc:tier[arc:art=current()/arc:art]) &lt; 2">In dieser Unterkunft gibt es weniger als zwei Tiere dieser Art.</report>   (3)
      <assert test="count(parent::*/arc:tier[arc:art=current()/arc:art][@geschlecht='männlich'])=1">Ein Paar muss immer aus einem Männchen und einem Weibchen bestehen.</assert>   (3)
    </rule>
  </pattern>
  <pattern>
    <rule context="arc:zimmer">
      <report role="info" test="count(arc:tier) &gt; 6">Noah, bringst du zu viele Tiere in einem Zimmer unter, könnte sich das schlecht auf die Zimmergemeinschaft auswirken! Du solltest nicht mehr als 6 Tiere in einem Zimmer unterbringen.</report>   (1)
    </rule>
  </pattern>
</schema> 

(1) Tests are assigned critical, ordinary error, warning or information categories using keywords fatal, error, warn or info.

(2) Since attribute role is attached to a <rule> element, its role value, here error, is inherited by all tests in that rule lacking a different role value.

(3) oXygen treats a test without attribute role as an ordinary error.

Defining different phases

Schematron phases contain sets of patterns for particular validation purposes. When a phase is selected, only rules in its active patterns are checked; inactive patterns are ignored.

If at least one phase was defined using phase, the implementation can ask the user to select a phase before validation. It should also offer a phase with every pattern active.

This permits a hierarchy similar to attribute role. For example, one phase shows only critical errors, another ordinary errors without warnings or information, and another hides only information.

Only complete patterns can be activated, not individual rules or tests. Two rules in one pattern must be active or inactive together. Organize rules into patterns with possible separate activation in mind.

First give IDs to all patterns the phase should activate. For simplicity, reduce our ark schema to two patterns:

<schema xmlns="http://purl.oclc.org/dsdl/schematron"> 
  <ns uri="http://www.schematron.info/arche" prefix="arc"/> 
  <pattern id="nutzlast">   (1) 
    <rule context="arc:nutzlast"> 
      <report test="number(//arc:nutzlast) &lt;  sum(//arc:gewicht)" role="fatal">Noah, du hast zu viele Tiere an Bord. Die Ladung überschreitet die Nutzlast deiner Arche.</report> 
    </rule> 
  </pattern> 
  <pattern id="zimmerGemeinschaft">   (2) 
    <rule context="arc:zimmer"> 
      <report test="count(arc:tier) &gt; 6" role="info">Noah, bringst du zu viele Tiere in einem Zimmer unter, könnte sich das schlecht auf die Zimmergemeinschaft auswirken! Du solltest nicht mehr als 6 Tiere in einem Zimmer unterbringen.</report> 
    </rule> 
  </pattern> 
</schema> 

(1) Use attribute id to give the payload pattern ID nutzlast. We plan a phase activating only this pattern.

(2) The other pattern needs no ID for this phase because it remains inactive, but for completeness it receives zimmerGemeinschaft.

Define a phase with top-level element <phase>. It needs attribute id so the implementation can reference it during selection. The ID is also its display name. Within <phase>, each <active> activates one pattern for that phase through attribute pattern, whose value must be that pattern’s ID.

For the shortened ark schema, define a phase activating only the payload pattern:

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <ns uri="http://www.schematron.info/arche" prefix="arc"/>
  <phase id="kritischeFehler">   (1)
    <active pattern="nutzlast"/>   (2)
  </phase>
  <pattern id="nutzlast">
    <rule context="arc:nutzlast">
      <report test="number(//arc:nutzlast) &lt; sum(//arc:gewicht)" role="fatal">Noah, du hast zu viele Tiere an Bord. Die Ladung überschreitet die Nutzlast deiner Arche.</report>
    </rule>
  </pattern>
  <pattern id="zimmerGemeinschaft">
    <rule context="arc:zimmer">
      <report test="count(arc:tier) &gt; 6" role="info">Noah, bringst du zu viele Tiere in einem Zimmer unter, könnte sich das schlecht auf die Zimmergemeinschaft auswirken! Du solltest nicht mehr als 6 Tiere in einem Zimmer unterbringen.</report>
    </rule>
  </pattern>
</schema> 

(1) Attribute id supplies the ID and display name. Choose a unique, meaningful value. Here, kritischeFehler was chosen because the phase activates rules covering all critical errors.

(2) Element <active> activates the pattern with ID nutzlast for this phase.

Let us see how oXygen handles the schema:

Phases in oXygen

Figure: phases in oXygen

Validation first displays a selection dialog offering phases #ALL and kritischeFehler. Phase #ALL is the built-in phase activating all patterns. Validation with #ALL is equivalent to validation without a selected phase. Choosing kritischeFehler as illustrated validates with phase kritischeFehler and hides information about overcrowded rooms.

Next create a phase for each error-hierarchy level in the full schema, with a default phase. oXygen preselects that default, though other implementations may differ.

<schema xmlns="http://purl.oclc.org/dsdl/schematron" defaultPhase="alleFehler">   (1)
  <ns uri="http://www.schematron.info/arche" prefix="arc"/>
  <phase id="kritischeFehler">
    <active pattern="nutzlast"/>
  </phase>
  <phase id="alleFehler">   (2)
    <active pattern="nutzlast"/>
    <active pattern="Repro"/>
    <active pattern="fressSchutz"/>
    <active pattern="vonJedemEinPaar"/>
  </phase>
  <phase id="keineInfos">   (2)
    <active pattern="nutzlast"/>
    <active pattern="Repro"/>
    <active pattern="fressSchutz"/>
    <active pattern="vonJedemEinPaar"/>
    <active pattern="gleichberechtigung"/>
  </phase>
  <pattern id="Repro">   (3)
    <rule context="arc:tier[@geschlecht='männlich']">
      <report test="number(arc:alter) &gt; number(//arc:maxReproduktionsalter /arc:tier_art[arc:name=current()/arc:art] /arc:männlich)" role="error">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) &gt; number(//arc:maxReproduktionsalter/arc:tier_art[arc:name=current()/arc:art]/arc:weiblich)"role="error">Das Weibchen ist zu alt, Noah! Es wird sich nicht mehr fortpflanzen können. Sorge für die natürliche Auslese.</report>
    </rule>
  </pattern>
  <pattern id="nutzlast">   (3)
    <rule context="arc:nutzlast">
      <report test="number(//arc:nutzlast) &lt; sum(//arc:gewicht)" role="fatal">Noah, du hast zu viele Tiere an Bord. Die Ladung überschreitet die Nutzlast deiner Arche.</report>
    </rule>
  </pattern>
  <pattern id="fressSchutz">   (3)
    <rule context="arc:tier[@fleischfresser='ja']" role="error">
      <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 &lt; (arc:gewicht div 2)">Noah, dieser Fleischfresser ist zu stark (schwer) für seinen Zimmergenossen. Er könnte ihn als Nahrungsquelle benutzen.</report>
    </rule>
    <rule context="arc:tier">
      <report test="parent::*/arc:tier/arc:gewicht &lt; (arc:gewicht div 10)" role="error">Noah, das Tier ist zu schwer für seine Zimmergenossen! Es könnte einen zertrampeln.</report>
    </rule>
  </pattern>
  <pattern id="vonJedemEinPaar">   (3)
    <rule context="arc:tier">
      <report test="count(parent::*/arc:tier[arc:art=current()/arc:art]) &lt; 2">In dieser Unterkunft gibt es weniger als zwei Tiere dieser Art.</report>
      <assert test="count(parent::*/arc:tier[arc:art=current()/arc:art][@geschlecht='männlich'])=1">Ein Paar muss immer (nur) aus einem Männchen und einem Weibchen bestehen.</assert>
    </rule>
  </pattern>
  <pattern id="gleichberechtigung">   (3) (4)
    <rule context="arc:tier">
      <report test="count(//arc:tier[arc:art=current()/arc:art]) &gt; 2"role="warn">In der Arche gibt es mehr als zwei Tiere dieser Art.</report>
    </rule>
  </pattern>
  <pattern id="zimmerGemeinschaft">   (3)
    <rule context="arc:zimmer">
      <report test="count(arc:tier) &gt; 6" role="info">Noah, bringst du zu viele Tiere in einem Zimmer unter, könnte sich das schlecht auf die Zimmergemeinschaft auswirken! Du solltest nicht mehr als 6 Tiere in einem Zimmer unterbringen.</report>
    </rule>
  </pattern>
</schema>


(1) Add to root element <schema> attribute defaultPhase with the phase ID as its value. Here the second phase becomes the default, with ID alleFehler.

(2) Also define a phase for all errors with ID alleFehler and one including warnings but excluding information with ID keineInfos.

(3) Give each pattern an ID.

(4) The warning restricting each species to one pair was previously in pattern vonJedemEinPaar. That pattern contains ordinary errors, while other phases must activate the warning separately. Create a new pattern containing only that warning as its <rule>. Warnings and ordinary errors now belong to different patterns.

oXygen now offers more phases, and their order differs from the order of <phase> elements in the schema:

Extended phases for the ark schema

Figure: extended phases for the ark schema

The default phase moves to the beginning and is preselected.

This is only one use of phase. Phases need not represent error priorities. Noah might appoint a species-protection officer responsible only for preventing extinctions during the voyage. Authorized to check only certain errors, that person gets a different view of the XML document.

   

 

 

 

back to top