XML technologies / XML / A language for validation
(Montero Pineda, Manuel: “Kontrollierende Sprache”, in: technische kommunikation, issue 4, 2018).
Anyone creating technical documentation with XML must also address quality and its control—for example, using a DTD. Alternatives exist, however.
Before XML documents enter further publishing processes, their validity should always be checked for quality assurance. Various schema languages can perform this check. Also called grammars, these languages govern such things as element order, where elements occur, how often they appear, and the data types of elements and attributes. Publishers and technical documentation authors often choose DTDs.
What is a DTD?
The abbreviation DTD stands for Document Type Definition. A DTD describes which elements exist in a project and which are optional or required. It can also define their order, permitted frequency, and attributes.
DTDs have existed for a long time, dating back to SGML. They are also ISO/W3C standards. Their great advantage is that they are very easy to learn.
How is it structured?
To illustrate DTD syntax, the following example describes the structure of a poetry collection.
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT Gedicht (Titel, Untertitel?, Strophe+)>
<!ELEMENT Titel (#PCDATA)>
<!ELEMENT Untertitel (#PCDATA)>
<!ELEMENT Strophe (Vers+)>
<!ELEMENT Vers (#PCDATA)>Code example: DTD structure illustrated by an example.
An element can contain other elements, for example <!ELEMENT Name (Unterelement)>, or text content <!ELEMENT NAME (#PCDATA)>. The element name must follow XML naming conventions.
DTD syntax uses commas for sequences. Here is a selection of the most important instructions: <!ELEMENT Name (Unterelement1, Unterelement2)>. This means that Unterelement1 must be followed by Unterelement2. Repetition is indicated with +: <!ELEMENT Name (Unterelement1)+>. This means that Unterelement1 must occur at least once, but may occur several times. Optional items are marked with either * or ?: * means zero, one or any number of occurrences; ? means one or zero. Alternatives are expressed with |. DTDs can also define element attributes in an attribute list— ATTLIST —and define value types (see the following example).
<!ELEMENT Name (#PCDATA | Unterelement1)*>
<!ATTLIST Name
id ID #REQUIRED
datum CDATA #IMPLIED>Code example: Defining attributes and value types in a DTD.
The example shows that the element Name has the attributes id and datum, which must contain a unique value and a string respectively. The next example shows how to invoke the sample DTD in XML.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Gedichtsammlung SYSTEM "Gedicht.dtd">
<Gedichtsammlung>
<Gedicht>
<Titel>Der Panther</Titel>
<Untertitel>Im Jardin des Plantes, Paris</Untertitel>
<Strophe>
<Vers>Sein Blick ist vom Vorübergehn der Stäbe</Vers>
<Vers>so müd geworden, daß er nichts mehr hält.</Vers>
<Vers>Ihm ist, als ob es tausend Stäbe gäbe</Vers>
<Vers> und hinter tausend Stäben keine Welt.</Vers>
</Strophe>
<Strophe>
<Vers>Der weiche Gang geschmeidig starker Schritte,</Vers>
<Vers> der sich im allerkleinsten Kreise dreht,</Vers>
<Vers> ist wie ein Tanz von Kraft um eine Mitte,</Vers>
<Vers>in der betäubt ein großer Wille steht.</Vers>
</Strophe>
<Strophe>
<Vers> Nur manchmal schiebt der Vorhang der <Fett>Pupille</Fett></Vers>
<Vers>sich lautlos auf -. Dann geht ein Bild hinein,</Vers>
<Vers> geht durch der <Fett>Glieder</Fett> angespannte Stille -</Vers>
<Vers>und hört im <Fett>Herzen</Fett> auf zu sein.</Vers>
</Strophe>
</Gedicht>
<Gedicht>
<Titel>Der Zauberlehrling</Titel>
<Strophe>
<Vers>Hat der alte Hexenmeister</Vers>
<Vers>Sich doch einmal wegbegeben!</Vers>
<Vers>Und nun sollen seine Geister</Vers>
<Vers>Auch nach meinem Willen leben.</Vers>
<Vers>Seine Wort und Werke</Vers>
<Vers>Merkt ich und den Brauch,</Vers>
<Vers>Und mit Geistesstärke</Vers>
<Vers>Tu ich Wunder auch.</Vers>
</Strophe>
...
</Gedicht>
</Gedichtsammlung>Code example: Invoking a DTD in XML.
What argues against DTDs?
DTDs are easy to learn, which is an advantage. Against that stand disadvantages, including these:
The standard is no longer developed.
Newer software often does not support DTDs.
They are not written in XML syntax.
Recursion cannot be ruled out.
Namespaces are inadequately supported.
There are hardly any data types.
A closed list of element contents is not possible.
What are the alternatives?
Instead of DTDs, XML document validity can also be checked using XML Schema. XML Schema is the W3C’s official successor to DTDs. Its schemas are more powerful, offering more control over data. Dozens of predefined data types exist, and you can define your own.
Other advantages of XML Schema over DTDs include easier descriptions of element content and document structure. Data contents can be checked better, and very precise definitions are possible. Schemas also use XML syntax and support namespaces, making data easier to reuse in other projects.
Another alternative is the ISO-certified schema language RELAX NG. Compared with XML Schema, it sometimes offers more validation possibilities, better syntax and an intuitive compact notation. However, few editors support RELAX NG and it seems not to be gaining acceptance.
The following table summarizes the relative advantages and disadvantages of DTD, XML Schema and RELAX NG.
Table: Schema languages compared.
DTD | XML Schema | RELAX NG | |
|---|---|---|---|
Adoption | ++ | +++ | - |
Expressive power | - | ++ | +++ |
Future viability | - | +++ | + |
Software support | + | ++ | - |
Data types | - | ++ | ++ |
Conclusion: DTDs are easy to learn. This is one advantage, but there are also several disadvantages.
Copyright © 2018 tcworld GmbH
You may print the online version for your private use.
Otherwise, this article from “technische kommunikation” (issue 4, 2018) is subject to the same conditions as the bound edition: the work, including all its parts, is 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