XML technologies / XML / Perl & XML / Introduction to XML / Declaring elements and attributes
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
If well-formedness is not enough and you need more precise control over document structure, you should express the grammar of your markup language in a DTD. These rules turn your markup into a formal language that no international organization could document better. A DTD lets you determine directly whether a document conforms to it. Such a document is a valid instance of the document type.
To describe the language, a DTD contains essentially two types of declaration. The first is the element declaration. It introduces a new permitted element name. It also uses a special language of template to specify what is allowed inside the element. Some examples:
<!ELEMENT Sandwich ((Schinken | Kaese)+ | (Erdnussbutter, Marmelade)), Wuerze+, Gurken?)>
<!ELEMENT Gurken EMPTY>
<!ELEMENT Wuerze (PCDATA | Senf | Ketchup )*> The first parameter gives the element name. The second is a template describing the content model (“content model”), meaning the data allowed inside the element, in parentheses, or the keyword EMPTY. Such a content model is simply a regular expression. The main difference is that entire element names, rather than individual symbols, are used as tokens. Commas also separate the individual elements. Every element appearing in the content model should be declared in the DTD.
In addition to element declarations, there is the attribute-list declaration. This assigns a set of optional or required attributes to an element. It also allows some control over attribute values, although the available constraints are limited. Consider another example:
<!ATTLIST Sandwich
id ID #REQUIRED
preis CDATA #IMPLIED
geschmack CDATA #FIXED "prima"
name (Legato | Schinken-Kaese | Solo | Duo ) 'Solo'
> An attribute declaration consists essentially of three parts: a name, a data type, and a more detailed specification. Our example declares the attributes of the element <Sandwich>. The first, named id also has the type ID. This means that the attribute value is a string unique among all other attribute values of the same type. This uniqueness applies only within the document. The keyword #REQUIRED indicates that this attribute must be present. The second attribute, named preis has the type CDATA. Because of the keyword #IMPLIED it is optional. The third attribute, named taste is fixed and always has the value "prima". This value cannot be omitted: all <Sandwich> elements inherit it automatically. Finally, the attribute name, which can only take values from a predefined list, has a default value of 'Solo'.
Despite their long history and successful use, both element and attribute declarations have significant drawbacks. Content-model syntax is relatively inflexible. For example, it is surprisingly difficult to express: “This element contains each of the subelements A, B, C, and D, in any order.” Try it yourself! There are also no powerful constraints on text data. For example, you cannot ensure that an element <Datum> actually contains a date rather than, say, a street name. Third, and perhaps most importantly, DTDs cannot be combined with namespaces. When declaring elements, you must declare every element you intend to use anywhere in the document, not just some of them. If, for example, you want to import some elements from another namespace you cannot also validate with a DTD, at least not without fixing the prefix and combining different DTDs, which does not work particularly well anyway.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions 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.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top