XML technologies / Schematron / Better quality in XML

(Montero Pineda, Manuel: Mehr Qualität in XML, in: technische kommunikation, issue 1, 2018).

XML Schema and DTD cannot check every XML quality problem. ISO Schematron can provide support.

Schematron validates XML content and structure, supporting quality assurance. It complements grammars such as XML Schema or DTD, solving quality problems they cannot check by expressing restrictive application rules. Rick Jelliffe developed Schematron in Taiwan in 1999. Since May 2006 it has been an official ISO standard, “ISO Schematron”.

Which problems can be solved?

XML Schema and DTD cannot check dependencies between attributes or their values. Noah’s ark illustrates the consequences in code example 1.

<arche>
  <zimmer>
    <tier fleischfresser="nein">
      <art>Zebra</art>
    </tier>
    <tier fleischfresser="ja">
      <art>Gepard</art>
    </tier>
  </zimmer>
</arche>

Code example 1: excerpt from the XML file Noah uses to fill the ark.

The XML fragment describes Noah filling the ark (<arche>), composed of rooms (<zimmer>) holding animals (<tier>). Each room should have two animals, but a zebra and cheetah unfortunately share one. A rule preventing carnivores and non-carnivores sharing a room could avoid this. Example 2 shows the schema and problem.

<xs:element name="zimmer">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="2" maxOccurs="unbounded" ref="tier"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Code example 2: schema excerpt showing dependencies that cannot be checked.

The ark has rooms requiring at least two animals, but XML Schema or DTD cannot check attribute-value dependencies, so cannot prevent carnivores and non-carnivores sharing one.

How does checking work?

Schematron checks content and structure using W3C’s XPath standard, which navigates and selects within XML. XPath specifies element attributes with different values.

Code example 3 shows an XPath query for the combination of two tier elements.

tier[@fleischfresser='ja'] and tier[@fleischfresser='nein']

Code example 3: Noah’s ark business rule.

The two tier elements with attribute fleischfresser now receive value ja and value nein.

What else can be checked?

  • Schematron checks dependencies between elements.

  • Recursive structures can also be constrained, for example preventing a footnote inside another footnote, or one fett element inside another of the same kind.

  • Text can also be queried, very powerfully using regular expressions.

  • Schematron checks tables too: a six-column table should not have a seven-cell row.

  • Various tools support Schematron. In Oxygen XML Editor, the illustrated schema consists of rules and error messages, with rules again written in XPath.

Schematron in Oxygen Editor

Figure: Schematron in Oxygen Editor.

One rule forbids three consecutive dots: use the corresponding single character instead, as the warning below requests. The rule is:

<assert test="(not(contains(.‚'...')))"

Stattdessen soll ein entsprechendes Zeichen benutzt werden. Der dazugehörige Warnhinweis lautet:

>Bitte das entsprechende Zeichen benutzen!!!</assert>

For directory-wide checking, choose Oxygen’s “Validate” menu, specify the schema and Schematron schema type, then start validation. All errors appear within seconds, linked directly to documents; clicking one opens the document at that location.

This makes editing easy. Errors can also appear alongside the document during editing, discussed below. Other supporting editors include XMLmind and Abortext Editor.

Restricting large standards

A simple technique restricts large XML standards such as DITA or DocBook. Schematron can check directories or run during editing in Oxygen. Enter three forbidden dots and validation immediately displays an error; correcting it removes the message.

The figure shows how to restrict large standards. It contains a topic consisting of a heading and two paragraphs. Element <apiname> is forbidden by the rule.

Excluding unwanted elements with Schematron

Figure: Schematron excludes unwanted elements.

If apiname is used anyway, Schematron reports it as forbidden at that position. Large standards can thus be restricted without programming, by excluding elements in advance through Schematron.

   

Copyright © 2018 tcworld GmbH
You may print the online version for private use.
Otherwise, this article from "technische kommunikation" (issue 1, 2018) 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.

tcworld GmbH, Rotebühlstraße 64, 70178 Stuttgart, info@tekom.de, www.tekom.de

back to top