XML technologies / XML / Perl & XML / Introduction to XML / Schemas
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
Several proposals for better schema languages aim to overcome the drawbacks of DTDs. The W3C recommends the language XML Schema, but it is only one of several competitors; another may fit your needs better. Whether a suitable CPAN module exists for your preferred language may be decisive.
Unlike DTD syntax, XML schemas are XML documents, easily edited with numerous XML tools. Their real power is precise control of document data, attractive where data quality matters as much as, or more than, structure. The example below could serve a survey where checking data types is essential.
Example: An XML schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<xs:annotation>
<xs:documentation>
Umfrage des Wiewunderlandes
Papierkramministerium, Edelsteinstadt
</xs:documentation>
</xs:annotation>
<xs:element name="Umfrage" type="Umfragentyp"/>
<xs:complexType name="Umfragentyp">
<xs:element name="Qualität" type="xs:decimal" minoccurs="0"/>
<xs:element name="Adresse" type="Adressentyp"/>
<xs:element name="Teilnehmer" type="Teilnehmertyp"/>
<xs:attribute name="Datum" type="xs:date"/>
</xs:complexType>
<xs:complexType name="Adressentyp">
<xs:element name="Hausnummer" type="xs:decimal"/>
<xs:element name="Straße" type="xs:string"/>
<xs:element name="Stadt" type="xs:string"/>
<xs:element name="Bundesland" type="xs:string"/>
<xs:attribute name="Postleitzahl" type="PLZtyp"/>
</xs:complexType>
<xs:simpleType name="PLZtyp" base="xs:string">
<xs:pattern value="[0-9]{5}"/>
</xs:simpleType>
<xs:complexType name="Teilnehmertyp">
<xs:element name="Teilnehmer" minOccurs="1" maxOccurs="20">
<xs:complexType>
<xs:element name="Vorname" type="xs:string"/>
<xs:element name="Nachname" type="xs:string"/>
<xs:element name="Alter">
<xs:simpleType base="xs:positive-integer">
<xs:maxExclusive value="200"/>
</xs:simpleType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:schema> The first line identifies the document as a schema and associates it with XML Schema’s namespace. The element <xs:annotation> documents the schema’s purpose and details. After documentation, we can enjoy declaring element types.
Start with the root element <Umfrage>. The declaration uses the element type <xs:element>. Its attributes specify the name “Umfrage” and type “Umfragentyp” for <Umfrage>. Unlike DTDs, content descriptions are often separate from declarations, improving reuse of generic types from which many elements derive. The actual content appears further down, in an element <xs:complexType> with name="Umfragentyp". A <Umfrage> optionally contains an element <Qualitaet> followed by required elements <Teilnehmer> and <Adresse>. The attribute Datum is also required.
Both the attribute Datum and the element <Qualitaet> have special schema types: date and decimal respectively. If a document <Umfrage> puts anything nonnumeric in the field <Qualitaet>, the schema reports an error. DTDs cannot offer this level of control.
Schemas check many types: bytes, floating-point values, integers of different sizes, binary and Boolean values, times and durations, IP addresses, URLs, IDs, IDREFs and other DTD-derived types, and ordinary free-text fields.
Element-type descriptions use facets to describe contents more precisely. In our example, the element <age> has type positive-integer with a maximum of 200, specified by the facet max-inclusive. Other facets include precision and scale for precision and range, encoding for character encoding, pattern for regular expressions, enumeration for enumeration, and max-length for maximum length.
The concept of patterns appears in the type Adressentyp. This defines the PLZtyp of a Postleitzahl as five digits: [0-9]{3}. When no existing type fits, define your own.
Schemas are an exciting new technology making XML much more useful, especially for data applications treating documents as forms to fill in. A full account of their uses and capabilities belongs in another book.
Other schema languages
Despite W3C endorsement, XML Schema is not alone in flexible validation. Many programmers prefer RelaxNG (see http://www.oasis-open.org/committees/relax-ng/) or Schematron (http://www.ascc.net/xml/resource/schematron/schematron.html). Their goals broadly agree but their philosophies differ. Perl implementations of the latter are on CPAN, so we will revisit it in XML basics: reading and writing.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” 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.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de