XML technologies / XML / Better to check
(Montero Pineda, Manuel: “Kontrolle ist besser”, in: technische kommunikation, issue 1, 2019).
An information chain cannot be streamlined without valid XML. XML Schema and DTDs can help. How do they differ, and which is more suitable?
XML Schema succeeds DTDs, introduced in A language for validation. Both describe and structure XML. XML Schema supports automation, documentation and validation. The W3C published its Recommendations in May 2002.
XML Schema and DTDs, like RELAX NG, are grammar-based schema languages. They govern element occurrence, order and frequency, and the data types of elements and attributes.
Comparing schema languages
Every DTD can be converted to a schema without information loss. Schemas are more powerful than DTDs, offering more control. XML Schema’s design emphasized data types, which represent kinds of information such as numbers and dates. XML is text-based, so their textual representations must be validated.
XML Schema and regular expressions
XML Schema has dozens of predefined types, such as xs:integer and xs:date. The table gives examples and values. You can also define your own types.
Type | Allowed value examples | Forbidden value examples |
|---|---|---|
1, 2, 3, -4 | abc, 1.5, 1.0 | |
1.0, -2.234 | abc, 3.4.1 | |
2018-09-17 | 17.09.2018 |
Table: Data type examples.
XML Schema controls data using regex and value ranges. Regex combine characters or character classes, such as digits or whitespace, to define precise string patterns. A regular expression validates or searches strings. Ranges restrict types: numeric and date types can have minimum and maximum values. This XML example contains unwanted content.
<?xml version="1.0" encoding="UTF-8"?>
<Personendaten xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Beispiel1_Personendaten.xsd">
<Vorname>Manuel</Vorname>
<Nachname>Montero</Nachname>
<PLZ>PLZ-69118</PLZ>
<Ort>Heidelberg</Ort>
<Geburtsdatum>31.02.1980</Geburtsdatum>
</Personendaten>Code example: XML with unwanted content.
The postal code should contain digits only and the birth date follow a standardized type. Neither does.
The next schema defines content more precisely. The line <xs:element name="PLZ" type="xs:integer"/> restricts postal codes to digits. With <xs:element name="Geburtsdatum" type="xs:date"/>, birth dates must follow the format xs:date.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Personendaten">
<xs:complexType>
<xs:sequence>
<xs:element name="Vorname" type="xs:string"/>
<xs:element name="Nachname" type="xs:string"/>
<xs:element name="PLZ" type="xs:integer"/>
<xs:element name="Ort" type="xs:string"/>
<xs:element name="Geburtsdatum" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>Code example: Defined content in an XML Schema document.
The correct XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Personendaten xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Beispiel1_Personendaten.xsd">
<Vorname>Manuel</Vorname>
<Nachname>Montero</Nachname>
<PLZ>69118</PLZ>
<Ort>Heidelberg</Ort>
<Geburtsdatum>1980-02-28</Geburtsdatum>
</Personendaten>Code example: Correct XML.
More XML Schema advantages
XML Schema compensates for DTD weaknesses, making it easier to:
Describe element content and document structure.
Check data contents.
Databases integrate more easily because they support similar types. Precise allowed strings and automatic association of types with XML nodes are possible. Supporting scripts can process nodes directly, as integers for example, without casting their text first.
Supporting namespaces makes reuse easier. Schemas themselves use XML syntax, allowing parsers to check their validity. They can also be modified or analyzed using XSLT. They can be created with any XML editor, have integrated documentation and excellent modularization.
XML Schema disadvantages
Large schemas are difficult to develop or maintain without specialized editors or development environments. DTD syntax is not XML-based, but relatively easy to learn.
Schemas are much more complex and require attention to many details, but provide versatility often needed for precise XML descriptions.
Validating at interfaces
XML Schema is now a minimum validation standard and should be preferred to outdated DTDs in new projects. Good editors such as Oxygen XML Editor or Altova XMLSpy help create schemas graphically. More important than choosing a language, however, is validating files at all.
Always validate XML exchanged between technologies or created by technical writers. Using XML Schema’s capabilities to create robust rules and enforce them consistently brings major long-term benefits. As German philosopher and educator Andreas Tenzer put it, “Quality is the product of a love of detail.”
Copyright © 2019 tcworld GmbH
You may print the online version for your private use.
Otherwise, this article from “technische kommunikation” (issue 1, 2019) has the same conditions as the bound edition: the work and all its parts are copyrighted. All rights reserved, including reproduction, translation, microfilming, and electronic storage and processing.
tcworld GmbH, Rotebühlstraße 64, 70178 Stuttgart, info@tekom.de, www.tekom.de