XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / General entity declarations
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
As you learned on the page References, XML predefines five entities:
<the less-than sign, or opening angle bracket (<)
&the ampersand (&)
>the greater-than sign, or closing angle bracket (>)
"straight double quotation marks (")
'the apostrophe, or single quotation mark (')
A DTD can define many additional entities, however. This is useful not only in valid documents but even in documents you do not intend to validate.
Entity references are declared using the declaration ENTITY in the DTD. It specifies the entity’s name, which must be an XML names, and its replacement text. For example, this declaration defines &super; as shorthand for superkalifragilistischexpiallegorisch:
<!ENTITY super "superkalifragilistischexpiallegorisch"> Once this is done, you can use &super; wherever you would otherwise type the entire word—and perhaps misspell it.
Entities can contain markup as well as text. For example, this declaration defines &footer; as shorthand for a standard web page footer repeated on many pages:
<!ENTITY footer '<hr size="1" noshade="true"/>
<font CLASS="footer">
<a href="index.html">O'Reilly Home</a> |
<a href="sales/bookstores/">O'Reilly Bookstores</a> |
<a href="order_new/">How to Order</a> |
<a href="oreilly/contact.html">O'Reilly Contacts</a><br>
<a href="http://international.oreilly.com/">International</a> |
<a href="oreilly/about.html">About O'Reilly</a> |
<a href="affiliates.html">Affiliated Companies</a>
</font>
<p>
<font CLASS="copy">Copyright 2004, O'Reilly Media, Inc.<br/><a href="mailto:webmaster@oreilly.com">webmaster@oreilly.com</a></font>
</p>
'>The entity’s replacement text must be well-formed. For example, you cannot put a start-tag in one entity and its matching end-tag in another. Quotation marks inside the replacement text must also differ from those delimiting it. Here, we used single quotes around the replacement text and double quotes inside. However, we had to replace the apostrophe in “O'Reilly” with the predefined general entity reference '. Replacement text may itself contain entity references, resolved before the text is substituted. Self-references and circular references are forbidden. General entities insert replacement text into the body of an XML document. They may also be used within a DTD where they eventually reach the document body, such as in default attribute values or another entity’s replacement text. They must not specify the text of the DTD itself. For example, this declaration is not allowed:
<!ENTITY koordinate "((x, y) | (y, x) | (θ, r) | (r, θ))" >
<!ELEMENT polygon (&koordinate;, &koordinate;, &koordinate;+)>We will shortly see how another kind of entity, the parameter entity, can achieve the intended result.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" 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