Mark Johnson
You're creating XML content and you’re repeatedly using some strings.
Or, maybe there are strings you would like to change from document to
document. Or, perhaps some strings in your documents, like copyright
years, change over time.
If you have any of these concerns, then general entities may be for
you. Inherited from SGML, a general entity allows you to define a
symbolic name for a chunk of text. You have likely already used general
entities without knowing it. The XML and HTML notation < is a
reference to the general entity lt, defined in the XML and HTML
specifications. It evaluates to a single left angle bracket, or less-
than sign ('<').
However, you may not have known that you can define your own entities
in your DTD, using the <!ENTITY> notation. Here's an example:
<pre>
<xml version="1.0"/>
<font color="red"><!-- Define general entities in the DTD --></font>
<!DOCTYPE Letter [
<font color="blue">
<!ENTITY recipient 'Mom'>
<!ENTITY closing 'Love'>
<!ENTITY relationship 'mother'>
<!ENTITY sender 'Your loving son'></font>
]>
<Letter>
Dear <font color="blue">&recipient;</font>,
You're the best <font color="blue">&relationship;</font> I
could ever hope for.
Can I borrow some money?
<font color="blue">&closing;</font>,
<font color="blue">&sender;</font>
</Letter>
</pre>
This document will be parsed as:
Dear Mom
You're the best mother I could ever hope for.
Can I borrow some money?
Love,
Your loving son
The <!ENTITY...> lines define entities, and entity names delimited
by ‘&' and ';' reference those entities. (Entity definitions,
entity references, and replacement text all appear in blue above.)
Parsers replace entity references with the text definitions of those
entities.
Entities can be used to manage parametric text in structured text
documents. So, if you don't get any money from Mom, try sending the
same letter, but change the DTD:
<pre>
<!DOCTYPE Letter [
<!ENTITY recipient 'Dad'>
<!ENTITY closing 'Cheers, Pop!'>
<!ENTITY relationship 'father'>
<!ENTITY sender 'Your favorite kid'>
]>
</pre>