(Excerpt from "The MathML Handbook" by Pavi Sandhu)
We saw earlier that the value of an attribute must be enclosed in quotation marks. However, if the value of an attribute itself contains a quotation mark, the XML processor may interpret the quotation mark as indicating the end of the attribute value. The appearance of a quotation mark in an attribute value is therefore an XML error, as shown here:
<person relation='Alice's friend'>
To prevent errors of this type, you can use one type of quotation mark inside the attribute value and the other type of quotation mark to enclose the entire value, as shown here:
<person relation="Alice's friend">
An alternative solution to the problem is to replace the quotation mark in the attribute value with the entity reference '. When the XML document is processed, the single quotation mark character replaces this entity reference, and there is no confusion about the start and end quotation marks that enclose the attribute value itself, as you can see here:
<person relation='Alice's friend;>
Another common use of an entity reference is to replace the < character when it occurs as part of an element's content. For example, a processor could interpret the < in the content of the inequality element here as the start of a new element (this leads to an XML error since there is no matching > to close the tag):
In cases like this, when the character data in an element includes a <, you must use the entity reference < to avoid inadvertently indicating the start of another element. For the above element to be interpreted correctly, you must represent it as follows:
XML includes five predefined entities that can be used to represent characters that have a special meaning in XML syntax (see the following table).
Table: Entities that represent characters with a special meaning.
Each entity reference consists of an ampersand (&) followed by a keyword followed by a semicolon (;). To include any of the characters in the right column in your document, you can insert the corresponding entity reference (in the left column) instead.
The use of entities in XML is not confined to these five characters. You can define your own entities to serve as an abbreviation for any piece of data, whether it be a single character or an entire file. Entities provide a convenient way to refer to any data that occurs frequently in a given document. The proper use of entities can make complex documents easier to read, write, and maintain.
For each entity that you wish to use, there must be a corresponding entity declaration in the DTD referenced by the document. An entity declaration is a special type of statement that associates a keyword with the piece of data that you want to represent. Each entity declaration has the following syntax:
<!ENTITY name string>
This associates the keyword name with the data specified by string. Then, anytime that data occurs in your document, you can use the text &name; in place of the actual data. Here is an example of an entity declaration:
<!ENTITY crm www.charlesriver.com>
This statement defines a named entity called crm to represent the URL of the Charles River Media Web site. You can then include a reference to this URL in an XML document, as shown below:
<message>For more information, visit &crm;.</message>
An XML processor will then interpret this markup as the following text.
<message>For more information, visit www.charlesriver.com.</message>
There are two ways to include an entity declaration for a given document:
- Add it to the external DTD referenced by the document. Doing this is appropriate if the entity is going to be used in many documents that use that DTD.
- Add it to the internal DTD subset declared in the body of the document itself. Doing this is appropriate if the entity reference is to be used in that particular document only.
The entities discussed in this section, which are used in ordinary XML documents, are called general entities. XML also defines another set of entities called parameter entities, which serve the same function but are used only in XML DTDs. We’re not discussing parameter entities since they are not relevant to the main theme of this book.
|<< back||next >>|
Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.
CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America