|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
CHAPTER 13
|
Table 13.1 The Predefined Entities | |
---|---|
Character | Replacement |
& | & or & |
| ' or ' |
> | > or > |
< | &lt; or &#38;#60; |
| &quot; or &#34; |
You will see that the table now gives you two options: You can enter a named entity, for example &apos;, or you can enter character reference &#39; to represent the character. The character references for the ampersand (&) and the less than (<) character are, however, special cases, and so the character references are double escaped; the reasons for this will be explained next when you learn about entity references.
You can think of entity references as being almost a kind of macro. They can be real time savers when you want to use sections of text several times. Consider the example shown below, where the persons name is an entity reference:
<?xml version=1.0"?> <!DOCTYPE home.page [ <!ENTITY club Antidisestablishmentarianism Club of America> ]> <home.page> <head> <title>The &club; Home Page</title> </head> <body> <h1>Welcome to the &club; Home Page!</h1> <para>Hello, I am the chairman of the &club; and this is our clubs Home Page...</para> </body> </home.page>
By using an entity reference in this way, you would only have to enter the name once, in the entity declaration, instead of having to search for and change every occurrence of the string in the text. Used in this way, entity references can make the task of creating and maintaining XML documents quite a bit easier.
Youve seen how handy internal entity references can be as a kind of shorthand for entering pieces of text and as a means of dealing with variable content. Obviously, with a little thought and advance preparation, entity references can save you a lot of time and effort later on.
A feature this handy naturally raises a simple question: Could this be used to insert markup, too? Its an attractive idea and a natural thing to want to do. You can put markup into replacement text, subject to a few restrictions. You need to think it out quite carefully beforehand to avoid some unpleasant surprises.
The first thing you must remember is that the XML will process the contents of the entity replacement text when it expands the entity reference. Therefore, you must not escape any markup characters in the replacement text, you must double escape the characters. Consider this simple example:
<!ENTITY dangerous Black &#38; White>
When the XML processor sees the entity reference &dangerous; in the XML document, it will immediately expand (de-reference) the predefined entity before it inserts the replacement text. The following XML code seems harmless enough, but well look at what happens, step by step:
<text>This is not a &dangerous; choice.</text>
<text>This is not a Black & White choice.</text>
Previous | Table of Contents | Next |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |