- 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).