Entity References Entity references are pointers to entities. In XML, entities are units of text, where each
unit could be anything from a single character to an entire document or even a reference
to another document. Instead of forcing you to merge all your entities into a single chunk,
entity references allow you to tell the parser where to go to find whatever entities you
want included. The parser then goes to that location, fetches its contents, and stuffs it
into the slot previously used by the entity reference. You could think of entity referencing
as something that enables you to stay object oriented in your approach: Make your
entities your objects and your entity references your pointers. Entity references can point
to both external and internal entities. Internal entities are usually defined in the DTD (see
the entity declarations portion later in this chapter). Figures 3.5 and 3.6 show how
external entity references work.
- 60 -
Figure 3.5: How entity references work—before parsing.
Figure 3.6: How entity references work—after parsing.
One of the most common uses of entity references is to include content that contains
characters that would otherwise confuse the parser. Once again, if you’ve worked a lot
with HTML, you’ve probably already used entity references in the shape of the “<” to
represent the less than sign (<).
For example, say you want to mathematically express that one of us authors is shorter
than the other; Alex is 6’ 2” and Faraz is 6’ 0”. Mathematically that would be:
Faraz < Alex
In XML, you might think to express it like this:
Faraz < Alex
But if you did that, the parser would get upset after reading Faraz because, as we
mentioned before, this isn’t well formed (there is a stray < character). In fact, you’d get an
annoying error message, depending on which parser you are using to read the file, and
- 61 -
wouldn’t be able to do any further processing (take a look at Figure 3.7).
Figure 3.7: Errors occur when you use illegal characters.
There are several ways of getting around this. One way is to use the entity references
built in to XML (expressed in Table 3.3).