Ever tried to read a DTD, and failed miserably? Ever wondered what all those symbols and weird language constructs meant? Well, fear not - this crash course will get you up to speed with the basics of DTD design in a hurry.
XML entities are a bit like variables in other programming languages - they're XML constructs which are referenced by a name and store text, images and file references. Once an entity has been defined, XML authors may call it by its name at different places within an XML document, and the XML parser will replace the entity name with its actual value.
XML entities come in particularly handy if you have a piece of text which recurs at different places within a document - examples would be a name, an email address or a standard header or footer. By defining an entity to hold this recurring data, XML allows document authors to make global alterations to a document by changing a single value.
An entity declaration typically looks like this:
<!ENTITY entityName entityValue>
Consider the following XML document,
<?xml version="1.0"?>
<article>
<title>The Fundamentals Of DTD Design)</title>
<abstract>A
discussion of DTD syntax and construction</abstract>
<body>
İright;
Article
body goes here
İright;
</body>
</article>
and then take a look at its DTD, which sets up the entity.
<!-- element declarations -->
<!ELEMENT abstract (#PCDATA)>
<!ELEMENT
article (title, abstract, body)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT title
(#PCDATA)>
<!-- entity declarations -->
<!ENTITY copyright "This material
copyright Melonfire, 2001. All rights
reserved.">
Entity declarations can contain XML markup in addition to ordinary text - the
following is a perfectly valid entity declaration:
<!ENTITY copyright "This material copyright <link>Melonfire</link>,
<publication_year>2001</publication_year>.
All rights reserved.">
Entities may be "nested"; one entity can reference another. Consider the following
entity declaration, which illustrates this rather novel concept.
<!ENTITY company "Melonfire">
<!ENTITY year "2001">
<!ENTITY copyright
"This material copyright &company;, &year;. All rights
reserved.">
Note, however, that an entity cannot reference itself, either directly or indirectly,
as this would result in an infinite loop (most parsers will warn you about this.) And now that I've said it, I just know that you're going to try it out to see how much damage it causes.