XML technologies / Schematron / Schematron reference
(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 10)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
<active>
For a phase this element activates exactly one pattern. The activation is done via the pattern attribute.
Parent element: <phase>
Child elements: <span>, <emph>, <dir>
Attribute:
pattern | With the help of this attribute, a pattern is referenced. |
Example: see <phase>.
<assert>
This element defines an assumption which has to be correct in order to ensure the Schematron validity. The context node for the assumption is determined by the enclosing rule. In the test attribute a XPath expression is indicated which is applied to the context node and by whom a Boolean value is expected. If the assumption is correct – which means the value true is returned – no error is generated. Otherwise the error message is outputted which is located within this element. With the help of the inline child elements <value-of> and <name>, the error message can be created variably to the context. As an alternative or in addition, reference can also be made to a proposal for solution with the diagnostics attribute.
Parent element: <rule>
Child elements: <dir>, <emph>, <span>, <value-of>, <name>
Attributes:
| The obligatory attribute contains a XPath expression. |
| With this attribute reference can be made to a proposal for solution. Depending on the implementation, the referenced proposal for solution is indicated in addition to the error message. Values: |
| Without this attribute an activated error message is always assigned to the context node. With the |
| This attribute indicates with a name a short description of the function within the pattern. The name can also be used for a sorting or a search. |
| By using a |
| Here, a URI /URL can be indicated which leads to further information on the appropriate element. An implementation can realise this, for example, by transforming a proposal for solution into a link which then leads to the indicated URI. |
| Assigns a system-independent ID value to the entire Schema, a pattern, a phase, a rule, an error, an assumption or a proposal for solution. |
| With the help of this attribute, elements can be visually grouped. By a reference to an image file, icons which can be depicted cohesively by the implementation are assigned to the appropriate elements. In this way, for example, a distinction can be made between a fatal or only a small error or a warning. |
| Assigns an ID value to the element. This value is used in order to refer to the element. The ID value has to be assigned uniquely to an element and must not be used by another element in the Schema as an ID. |
Example:
<rule context="ark:animal">
<assert test="count(parent::*/ark:animal[ark:species=current()/ark:species])=2">
There are less or more than two animals of this species in this accommodation.</assert>
<assert test="count(parent::*/ark:animal[ark:species=current()/ark:species][@sex='male'])=1">
A pair must always consist of one male and one female.</assert>
</rule>Since <assert> elements represent assumptions, in this rule it is determined for the first test that the number of animals which are accommodated in the same room and are of the same species must be exactly two. The second test determines that one animal of this pair is a male so that the second animal has to be a female.
<diagnostic>
The element defines a single proposal for solution. The reference results from the textual content of the element.
Parent element: <diagnostics>
Child elements: <dir>, <emph>, <span>, <value-of>
Attributes:
| See the |
| See the |
| See the |
| See the |
Example:
<pattern>
<rule context="ark:animal">
<report test="count(parent::*/ark:animal[ark:species=current()/ark:species]) > 2" diagnostics="number">
There are more than two animals of this species in this accommodation.</report>
</rule>
</pattern>
<diagnostics>
<diagnostic id="number">
Noah, you must remove as many animals from the ark so that
only two of one species live in this accommodation.</diagnostic>
</diagnostics>The <report> element refers to a <diagnostic> element. If an error is caused at this position, the proposal for solution is indicated. The error description could be as follows: »There are more than two animals of one species in this accommodation«.
Proposal for solution: »Noah, you must remove as many animals from the ark so that only two of one species live in this accommodation«.
<diagnostics>
This top-level element generates a container for proposals for solution. Each <diagnostic> element represents one proposal for solution to which reference can be made during a test (<assert> or <report>).
Parent elements: <schema>, <pattern>
Child elements: <include>, <diagnostic>
<dir>
This inline element can indicate the writing direction in text passages.
Parent elements: <p>, <diagnostic>, <active>, <title>, <assert>, <report>
Child elements: –
Attribute:
| With the help of this attribute, the writing direction to be used is indicated. |
<emph>
This inline element can emphasise text passages. If this element is supported, the implementation emphasises the appropriate text passage with the help of typographical means. It is optional which typographical means are to be used – it depends on the implementation.
Parent elements: <p>, <diagnostic>, <active>, <assert>, <report>
Child element: –
<extends>
With this element an abstract rule can be called up by using the rule attribute.
Parent element: <rule>
Child element: –
Attribute:
| With the help of this obligatory attribute, reference is made to an abstract rule by using an appropriate ID. |
Example:
<rule context="ark:animal">
<extends rule="weight"/>
</rule>
<rule abstract="true" id="weight">
<report test="parent::*/ark:animal/ark:weight < (ark:weight div 10)">
Noah, the animal is too heavy for its roommates!
It could trample down one of them.</report>
</rule>
<include>
With the help of this element, Schematron elements stored in external XML documents can be included into a Schematron schema. Depending on the context, for example, a <diagnostics> element, a pattern, a phase, a rule or a test (<assert> or <report>) can be embedded. By using the href attribute, reference is made to a Schematron schema file. This Schematron file must contain the element to be embedded as a root element.
Parent elements: <schema>, <diagnostics>, <pattern>, <phase>, <rule>
Child element: –
Attribute:
| The obligatory |
Example:
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<ns uri="http://www.schematron.info/arche" prefix="ark"/>
<pattern>
<rule context="ark:animal">
<report test="count(parent::*/ark:animal[ark:species=current()/ark:species]) > 2" diagnostics="number">
There are more than two animals of this species in this accommodation.</report>
</rule>
</pattern>
<include href="diagnostics.xml"/>
</schema>The schema contains an <include> element. It refers to the file below:
<diagnostics xmlns="http://purl.oclc.org/dsdl/schematron">
<diagnostic id="number">
Noah, you must remove as many of the animals so that only two
of one species live in this accommodation.</diagnostic>
</diagnostics>The schema below is identical to the schema above. The <include> element is replaced by the <diagnostics> element – the root element of the diagnostics.xml file.
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<ns uri="http://www.schematron.info/arche" prefix="ark"/>
<pattern>
<rule context="ark:animal">
<report test="count(parent::*/ark:animal[ark:species=current()/ark:species]) > 2" diagnostics="number">
There are more than two animals of one species in this accommodation.</report>
</rule>
</pattern>
<diagnostics>
<diagnostic id="number">
Noah, you must remove as many of the animals so that only two of one species
live in this accommodation.
</diagnostic>
</diagnostics>
</schema>
<let>
This element declares a variable. For this purpose, the variable name is indicated with the name attribute and the variable value is indicated with the value attribute by a XPath expression. Within a rule, the XPath expression is analysed by the context node. Simultaneously, the rule also determines the scope of application of the variable. It depends on the query language how the call is syntactically structured. In XPath a variable consists of the variable name and a prepended $ character. The call of the variable with the variable name name would be as follows: $name.
Parent elements: <schema>, <pattern>, <phase>, <rule>
Child element: –
Attributes:
| Defines the variable name. |
| The XPath expression indicated here determines the value of the variables. The expression is analysed in the context of the variable definition. |
Example:
[...]
<room>
<animal sex="female" carnivore="yes">
<species>lion</species>
<weight>200</weight>
<age>30</age>
</animal>
</room>
[...]<rule context="ark:animal">
<let name="animalSpecies" value="ark:species"/>
<report test="count(parent::*/ark:animal[ark:species=$animalSpecies]) > 2">
There are more than two animals of this species in this accommodation.</report>
<assert test="not(count(parent::*/ark:animal[ark:species=$animalSpecies]) < 2)">
There is no pair of this species in this accommodation.</assert>
</rule>The animalSpecies variable has been defined for the <rule> element. It contains the content of the ark:species element of the appropriate context node (ark:animal). The variable can be used in each XPath expression within the rule with the prepended $ character (count(parent::*/ark:animal[ark:species=$animalSpecies]) > 2).
<name>
This element is, without a further attribute, a short form of the <value-of> element to which the name() XPath expression is passed on in the select attribute. The element always outputs the element name of the context node and is used in order to create error messages (<assert> or <report>) or proposals for solution variably to the context node.
Parent elements: <assert>, <report>
Child element: –
Attribute:
| If the |
Example:
[...]
<room>
<animal sex="female" carnivore="no">
<species>zebra</species>
<weight>200</weight>
<age>40</age>
</animal>
<animal sex="male" carnivore="no">
<species>zebra</species>
<weight>250</weight>
<age>40</age>
</animal>
<animal sex="male" carnivore="no">
<species>zebra</species>
<weight>280</weight>
<age>40</age>
</animal>
</room>
<room>
<animal sex="female" carnivore="yes">
<species>lion</species>
<weight>200</weight>
<age>30</age>
</animal>
</room>
[...]<rule context="ark:animal">
<let name="animalSpecies" value="ark:species"/>
<report test="count(parent::*/ark:animal[ark:species=$animalSpecies]) > 2">
There are more than two <name/> elements of this species in this
<name path="parent::*"/> element.</report>
<assert test="not(count(parent::*/ark:animal[ark:species=$animalSpecies]) < 2)">
There is no further <name/> element of this species in this
<name path="parent::*"/> element.</assert>
</rule>The resulting error message of the first error is as follows: »There are more than two animal elements of this species in this room element.« The second error message would be as follows: »There is no further animal element of this species in this room element.«
<ns>
This element declares a namespace and the prefix to be used. If an element of the XML instance shall be addressed which is not in the Null namespace, a namespace has to be defined with this element.
Parent element: <schema>
Child element: –
Attributes:
| Here, the namespace URI is indicated for which a prefix shall be defined. |
| Here, a prefix is passed on which can now be used in all XPath expressions of the schema. It should be noted that there is no default namespace in XPath. Thus, a prefix must be passed on to each namespace. Elements without prefix are automatically assigned to the Null namespace. |
Example:
<ark xmlns="http://www.schematron.info/arche">
[…]
</ark><schema xmlns="http://purl.oclc.org/dsdl/schematron">
<ns uri="http://www.schematron.info/arche" prefix="ark"/>
<pattern>
<rule context="ark:room">
[…]
</rule>
</pattern>
</schema>In the instance the namspace "http://www.schematron.info/arche" is used. Therefore, this namespace has also to be defined with the <ns> element for the schema and a prefix has to be determined. Now, the prefix or the namespace can also be used in the XPath expression of the context attribute.
<p>
If this element is supported by the implementation, the content is presented in the form of a paragraph at the appropriate position. Consequently, a support makes only sense for an implementation which generates a central error report. An interactive implementation, like the one of the oXygen Editor, does not support this element.
Parent elements: <schema>, <pattern>, <phase>
Child elements: <span>, <emph>, <dir>
Attributes:
| See the |
| See the |
| See the |
Example:
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<title>Schematron for Noah</title>
<p>This Schematron schema shall help Noah to accommodate the right animals in the suitable environment.</p>
[…]
</schema>The contents of the elements <p> and <title> can be additionally presented by the implementation.
<param>
With the help of this element, a parameter can be passed on for a call of an abstract pattern. With the help of the value attribute, the value of the parameter can be indicated. In contrast to a variable, a XPath subexpression is passed on which is only resolved when calling up the parameter. A parameter is called up within a XPath expression by the key character $ with the parameter name. The parameter is determined via the name attribute.
Parent element: <pattern>
Child element: –
Attributes:
| See the |
| The string indicated here is the value of the parameter and represents a XPath subexpression. The expression is only analysed in the context of the call. |
Example:
<pattern id="noah" is-a="abstractPattern">
<param name="count" value="parent::*/ark:animal[ark:species=current()/ark:species]"/>
</pattern>
<pattern id="abstractPattern" abstract="true">
<rule context="ark:animal">
<report test="count($count) > 2">
There are more than two animals of this species in this accommodation.</report>
<assert test="not(count($count) < 2)">
There is no pair of this species in this accommodation.</assert>
</rule>
</pattern>The noah pattern calls with the is-a attribute the pattern with the abstractPattern ID. In doing so, the $count parameter is passed on. The parameter contains the XPath expression parent::*/ark:animal[ark:species=current()/ark:species]. Now, the abstract pattern with the abstractPattern ID is executed.
<pattern>
This element is grouping Schematron rules. The following shall apply for a pattern: Each node of the XML document to be checked shall never be checked for several matching rules in one pattern. If several rules of a pattern match a node, this node is checked by the first matching rule. The arrangement for the activation of certain rules also applies (see <active>). Here, not individual rules are activated, but only entire patterns.
Parent element: <schema>
Child elements: <include>, <title>, <p>, <let>, <param>, <rule>
Attributes:
| See the | ||||
| See the | ||||
| See the | ||||
| See the | ||||
| An abstract pattern can be called up by other patterns with the
| ||||
| With this attribute an abstract pattern is called up. The value of the attribute refers to the abstract pattern. |
Example:
<room>
<animal sex="female" carnivore="no">
<species>elephant</species>
<weight>900</weight>
<age>150</age>
</animal>
<animal sex="male" carnivore="no">
<species>elephant</species>
<weight>1200</weight>
<age>162</age>
</animal>
<animal sex="female" carnivore="no">
<species>zebra</species>
<weight>100</weight>
<age>40</age>
</animal>
</room>
<room>
<animal sex="male" carnivore="yes">
<species>lion</species>
<weight>280</weight>
<age>40</age>
</animal>
<animal sex="female" carnivore="yes">
<species>lion</species>
<weight>130</weight>
<age>16</age>
</animal>
</room><pattern>
<rule context="ark:animal[@carnivore='yes']">
<report test="parent::*/ark:animal/ark:weight < (ark:weight div 2)">
Noah, this carnivore is too strong (heavy) for its roommate.
The carnivore could use it as a food source.
</report>
</rule>
<rule context="ark:animal">
<report test="parent::*/ark:animal/ark:weight < (ark:weight div 10)">
Noah, the animal is too heavy for its roommates!
It could trample down one of them.</report>
</rule>
</pattern>The first rule queries whether the carnivorous animal is heavier than another animal in the same room. Animals may be maximally ten times as heavy as its rommates. For this purpose, the second rule checks each <animal> element. But since the first rule always applies to <animal> elements being carnivores, the second rule only queries the non-carnivorous animals.
<phase>
This element defines a phase. With the help of different <active> elements, certain patterns can be activated. Now, the user can choose a phase by an implementation. The document is only checked by means of the rules in the active patterns.
Parent element: <schema>
Child elements: <let>, <p>, <include>, <active>
Attributes:
| See the |
| See the |
| See the |
| See the |
Example:
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<phase id="all">
<active pattern="ark"/>
<active pattern="noah"/>
</phase>
<phase id="accommodation">
<active pattern="ark"/>
</phase>
<phase id="pairs">
<active pattern="noah"/>
</phase>
<pattern id="ark"> […] </pattern>
<pattern id="noah"> […] </pattern>
</schema>With this implementation the user can validate the instance according to the phases all, accommodation and pairs. At the all phase the patterns ark and noah are activated, at the accommodation phase only the ark pattern and at the pairs phase only the noah pattern. A phase which activates all patterns is usually provided by the implementation even without an all phase.
<report>
This element contains a test for an error. With the test attribute a XPath expression is indicated which is applied to the context node and from which a Boolean value is expected. If the false value is returned, no error message is generated. Otherwise the error message is outputted being indicated within this element.
Parent element: <rule>
Child elements: <dir>, <emph>, <span>, <value-of>, <name>
Attributes:
| See the |
| See the |
| See the |
| See the |
| See the |
| See the |
| See the |
| See the |
| See the |
Example:
<rule context="ark:animal">
<report test="count(//ark:animal[ark:species=current()/ark:species]) > 2">
There are more than two animals of this species on the ark.</report>
<report test="count(parent::*/ark:animal[ark:species=current()/ark:species]) < 2">
There are less than two animals of this species in this accommodation.
</report>
</rule>The first <report> test checks whether there are not more than two animals of the same species on the ark. Whereas the second test ensures that not less than two animals of a species are accommodated in one room.
<rule>
This element defines a Schematron rule. If there is a node in the XML document which matches the XPath expression indicated in the context attribute, all tests (<assert> or <report>) of this rule are executed with this node as a context node. One exception is a preceding rule in the same pattern which already applies to the same node. Then, no further rule of the current pattern is executed for this node.
Parent element: <pattern>
Child elements: <include>, <let>, <assert>, <extends>, <report>
Attributes:
| With this attribute a Schematron rule is transformed into an abstract rule. An abstract rule has no context and can be called up by other rules with the
| ||||
| See the | ||||
| See the | ||||
| See the | ||||
| See the | ||||
| With the help of a XPath expression, the context node of the rule is determined. Any rule being not abstract requires a context node which is used for all XPath expressions within this rule. | ||||
| See the | ||||
| See the | ||||
| See the |
Example:
<load>
<room>
<animal sex="female" carnivore="yes">
<species>lion</species>
<weight>200</weight>
<age>30</age>
</animal>
<animal sex="male" carnivore="no">
<species>zebra</species>
<weight>280</weight>
<age>40</age>
</animal>
</room>
</load><rule context="ark:room[ark:animal[@carnivore='no']]">
<report test="ark:animal[@carnivore='yes']">
There are carnivores and herbivores in one accommodation.
The animals are not a food source!
</report>
</rule>All tests of a rule are checked by appropriate context nodes. The respective context node for this rule is a node of the instance which matches the XPath expression ark:room[ark:animal[@carnivore='no']]. It must, therefore, be a <room> element which contains an <animal> element with the attribute value no in the carnivore attribute. In the specified instance the tests are only applied to the first two <room> elements. However, the report test only applies to the first <room> element – as a consequence, the user does not notice the check of the second <room> element.
<schema>
<schema> is the root element of a Schematron schema. It includes the entire schema and contains all patterns, proposals for solution and phases.
Parent element: –
Child elements: <diagnostics>, <include>, <let>, <ns>, <p>, <pattern>, <phase>, <title>
Attributes:
| With this attribute a default phase can be determined for a Schematron schema. If this attribute is supported, the implementation uses the phase referenced here per default. | ||||||||||||||||
| See the | ||||||||||||||||
| See the | ||||||||||||||||
| See the | ||||||||||||||||
| With the name of a query language it can be embedded for the use within the schema. For a successful embedding, the appropriate query language must be supported by the implementation. By the standard certain names are reserved for the most important languages.
Further query languages can be supported depending on the implementation. | ||||||||||||||||
| This attribute indicates the Schematron version. The application depends on the implementation. | ||||||||||||||||
| See the |
<span>
This inline element can highlight text passages. If this element is supported, the implementation highlights the appropriate text passage by using typographical means. The class attribute determines which typographical means are used.
Parent elements: <p>, <diagnostic>, <active>, <assert>, <report>
Child element: –
Attribute:
| The attribute determines the type of the typographical markup. There are no defaults for the values of the attribute. They are specified by the implementation alone. |
<title>
If this element is supported, the content is presented in the form of a title in the appropriate place. Consequently, a support is only reasonable for an implementation which generates a central error report. An interactive implementation, like the oXygen XML Editor, does not support such an element.
Parent elements: <schema>, <pattern>
Child elements: <dir>, <p>
<value-of>
This element equates with the <xsl:value-of> XSLT element. By using the select attribute, a XPath expression is passed on which is analysed and outputted by the context node. This element is used in order to create error messages (<assert> or <report>) or proposals for solution variably to the context node or to the document. In a simple case, the content of an element can be outputted from the instance.
Parent elements: <diagnostic>, <assert>, <report>
Child element: –
Attribute:
| The XPath expression indicated here determines the content to be inserted for the |
Example:
<room>
<animal sex="female" carnivore="no">
<species>zebra</species>
<weight>200</weight>
<age>40</age>
</animal>
<animal sex="male" carnivore="no">
<species>zebra</species>
<weight>250</weight>
<age>40</age>
</animal>
<animal sex="male" carnivore="no">
<species>zebra</species>
<weight>280</weight>
<age>40</age>
</animal>
</room><rule context="ark:animal">
<report test="count(parent::*/ark:animal[ark:species=current()/ark:species]) > 2">
There are more than two animals of this species in this accommodation (<value-of select="ark:species"/>).</report>
</rule>
Each <value-of> element within the tests outputs the zebra string.
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