XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / Parameter entities
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
Elements often share all or part of their attribute lists and content models. Every simple XLink element, for example, has attributes xlink:type and xlink:href, possibly also xlink:show and xlink:actuate. In XHTML, the elements th and td have much the same content. Repeating models or attribute lists in declarations is tedious and error-prone. It is easy to add a new child to some declarations and forget others.
Imagine a property-listing application with separate elements for apartments, accessory dwellings, lofts for sale, flats and houses. Declarations might look like this:
<!ELEMENT apartment (adresse, größe, zimmer, bäder, miete)>
<!ELEMENT einliegerwohnung (adresse, größe, zimmer, bäder, miete)>
<!ELEMENT loft (adresse, größe, zimmer, bäder, preis)>
<!ELEMENT wohnung (adresse, größe, zimmer, bäder, preis)>
<!ELEMENT haus (adresse, größe, zimmer, bäder, preis)>Much text overlaps. Adding another element such as verfügbarkeitsdatum later would require changing all five declarations. Better define a constant holding the shared content for all five listing types and reference it in each model. Adding or removing content then requires changing only the constant.
An obvious candidate is an entity reference. But general entities cannot replace parts of content models or attribute lists, only parts of the DTD inserted into the document itself. Instead XML offers a DTD-only construct: a parameter entity, referenced through a parameter entity reference. These behave and are declared much like general entities, but use % instead of & and work only in DTDs, while general entities work only in document content.
Parameter entity syntax
A parameter entity is declared almost like a general entity, adding a percent sign between <!ENTITY and the entity name:
<!ENTITY % immobilien_inhalt "adresse, größe, zimmer, bäder">
<!ENTITY % miete_inhalt "miete">
<!ENTITY % kaufpreis_inhalt "preis">Dereferencing likewise uses a percent sign instead of ampersand:
<!ELEMENT apartment (%immobilien_inhalt;, %miete_inhalt;)>
<!ELEMENT einliegerwohnung (%immobilien_inhalt;, %miete_inhalt;)>
<!ELEMENT loft (%immobilien_inhalt;, %kaufpreis_inhalt;)>
<!ELEMENT wohnung (%immobilien_inhalt;, %kaufpreis_inhalt;)>
<!ELEMENT haus (%immobilien_inhalt;, %kaufpreis_inhalt;)>Reading these declarations, the parser substitutes replacement text. To add the element verfügbarkeitsdatum to all five content models, extend the entity immobilien_inhalt as follows:
<!ENTITY % immobilien_inhalt "adresse, größe, zimmer, bäder, verfügbarkeitsdatum">This also works for attribute types and element names.
This trick is limited to external DTDs. Internal subsets forbid parameter references within only part of a markup declaration. They can, however, insert one or more complete declarations, typically through external parameter entities.
Redefining parameter entities
Redefinition makes these references especially powerful. When both subsets exist, the internal subset can give entities new replacement text. If the external subset’s ELEMENT and ATTLIST declarations use parameter references rather than literal names, the internal subset can change the DTD. For example, a document could add a schlafzimmer child to listings by redefining the entity immobilien_inhalt like this:
<!ENTITY % immobilien_inhalt "adresse, größe, zimmer, schlafzimmer, bäder, verfügbarkeitsdatum">When entity declarations conflict, the first wins. The parser reads the internal subset first, so its definition of immobilien_inhalt is used. In the external subset, every declaration containing immobilien_inhalt then gains a child schlafzimmer otherwise absent.
Modular XHTML, discussed in XML on the web, uses this extensively to let documents use only the HTML subset they need.
External DTD subsets
Real DTDs can be complex. The SVG DTD exceeds 1,000 lines; XHTML 1.0 Strict, the smallest of the three XHTML DTDs, exceeds 1,500. These are only medium-sized. The DocBook XML DTD exceeds 11,000 lines. Understanding and modifying such a long monolithic file is difficult.
Fortunately, DTDs can be split. DocBook has 28 parts for areas such as tables, entity declarations and notations. At validation, these are combined using external parameter entity references.
An external parameter entity uses an ordinary ENTITY declaration with a percent sign. Rather than literal replacement text, it supplies the keyword SYSTEM followed by a URL for the DTD part. This ENTITY declaration defines an external entity named namen whose content comes from the relative URL namen.dtd. The reference %namen; inserts that file into the current DTD:
<!ENTITY % namen SYSTEM "namen.dtd">
%namen;Relative or absolute URLs are allowed. Relative ones are usually more convenient.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” 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