XML technologies / XML / Perl & XML / Introduction to XML / Entities
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
XML offers another useful aid to authors: entities. An entity can stand for text to replace later, repeated text or difficult characters. It is XML that notionally stands outside the document. (Note: the whole document can itself be an entity, called the document entity, though ordinary usage favors parts of documents.) Insert its value with an entity reference. The processor resolves references while reading, replacing them with declared values. Referenced entities must therefore be declared somewhere.
Declare entities in the Document Type Definition (DTD). A DTD has two parts: the internal subset belongs to this document; the external subset is in another. “The DTD” often means the external part, though both make up the DTD. Entity declarations work in either. The next document demonstrates them.
Example: Entity declarations and references
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE memo
SYSTEM "/xml-dtds/memo.dtd"
[
<!ENTITY firmenname "Siggi Sauers Schokoladenfabrik">
<!ENTITY krankenkasse SYSTEM "kk.txt">
]>
<memo>
<to>An alle Sauer-Mitarbeiter</to>
<para>
&firmenname; hat einen neuen Eigentümer und Vorstandsvorsitzenden, nämlich Konrad
Korb. Da unser Markenname "&firmenname;" gut bekannt ist, soll er bewußt so
bleiben, wie er ist. Allerdings empfiehlt Hr. Korb dringend, die Konditionen der
Betriebskrankenkasse zu überarbeiten. Details über die neuen Konditionen finden Sie
im folgenden Text. Hr. Sauer läßt allen Mitarbeitern seinen Dank für die
langjährige Treue ausrichten und wünscht dem neuen Vorsitzenden und der Firma
&firmenname; alles Gute!
</para>
&krankenkasse;
</memo> Let us examine the new features. The DTD starts with special markup giving useful information, including the internal subset and path to the external one. Declarations defining something begin with an exclamation mark and keyword, here DOCTYPE. Next comes the element containing the document itself, called the root element or document element. Then comes the external path, SYSTEM "/xml-dtds/memo.dtd", and the internal subset in square brackets ([ ]).
External subsets normally hold declarations shared by documents. Internal ones hold document-specific declarations, including additions and overrides. Our internal subset has two entities: firmenname and krankenkasse. Each declaration needs a name and replacement text.
These are general entities, distinguished from other types because the document author declares them. Replacement text has two sources: directly in the declaration, as in the first example, or a file, as in the second. The latter uses a system identifier to specify a path, like a browser URL. The processor loads the file and inserts it at the reference. This is an external entity .
Notice markup like &name;. The ampersand (&) marks a reference, while name names the entity. Repeated references avoid repeated text, as with firmenname.
Entities can contain text or markup, as in krankenkasse. We do not know that file’s exact content, but its size suggests both. Entities can include other entities at any depth, but must never refer to themselves directly or indirectly: circularity would create an endless input stream. Some XML technologies, such as XSLT, support recursion, but entity references do not. Think of them as constants in source code; a compiler would disapprove there too.
Another reference, ß, must be declared externally. Imagine an editor unable to enter ß. (Note: ignore its ability to handle umlauts. The English original makes this example more natural than the German translation. Umlauts can always cause trouble, even simply by forgetting encoding="iso-8859-1", so retaining this example seems sensible.—Translator’s note.) Since this entity inserts one character, it is an alias. XML’s usual mechanism is a numeric character entity, here ß. 0x00DF is hexadecimal for 223, the code for ß in Unicode, XML’s native character set, discussed further in Unicode, character sets and encodings.
Short descriptive names are easier to remember than 00DF, so users add aliases to DTDs like this:
<!ENTITY % szlig ß>XML also has five built-in references listed below, really escape sequences for special characters rather than ordinary references.
Table: Entity references valid without declarations
Character | Entity |
| |
| |
| |
| |
| |
Especially important are < and &; the others are rarer. A parser cannot distinguish when < means mathematical “less than” or starts an element. It always assumes the latter, possibly reporting malformed XML.
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
back to top