[Transfer] http://www.xml.com/pub/a/98/08/xmlqna1.html#INTENT
Types of Entities
By Norman Walsh
August 28,199 8
Internal Entities
Do you ever get tired of typing the name of your company, "Yoyodyne
Industries, Inc ."? Have you ever had the pleasure of spelling it incorrectly
In an important document? Internal entities offer a convenient solution
These problems.
Instead of typing the same text over and over again, you can define
An internal entity to contain the text and then you only need to use the entity
Where you want to insert the text. Because the entity is expanded by the parser,
You can be assured that you'll get the same text in every location. The parser
Will also catch typos if you misspell an entity name (so long as there's no
Entity name that matches your typo !).
To use an entity you insert an "entity reference" into your
Document. You're probably already familiar with some entity references because
You need to use them for special characters that cannot be typed directly
In an XML document, like "<" and "&". An entity
Reference is an ampersand (&), followed by the name of the entity, followed
By a semicolon (;).
If you 've defined the entity"Yoyo"
Contain the name of your company, then you can use it with the following entity
Reference"& Yoyo;".
The text that is inserted by an entity reference is called the "replacement
Text ". The replacement text of an internal entity can contain markup
(Elements, attributes, processing instructions, other entity references, etc .),
But the content must be balanced (any element that you start in an entity
Must end in the same entity) and circular entity references are not allowed.
You create internal entities with entity declarations in the internal subset or the DTD.
Predefined Entities
Five internal entities are predefined in XML:
Table 1. Predefined Entities
Entity Name |
Replacement Text |
Lt |
The less than sign (<) |
Gt |
The greater than sign (>) |
Amp |
The ampersand (&) |
Apos |
The single quote or apostrophe (') |
Quot |
The double quote (") |
All XML processors are required to support references to these entities,
Even if they are not declared.
Character References
Character references, which are similar in appearance to entity references,
Allow you to reference arbitrary Unicode characters, even if they aren't available
Directly on your keyboard. Character references are not properly entities
At all.
Character references are numeric and can be used without any special
Declaration.
The basic format of a character reference is either"&#Nnn;"Or"& # XHhh;"Where"Nnn"
Is a decimal Unicode character number and"Hhh"
Is a hexadecimal Unicode character number.
A character reference inserts the specified Unicode character directly
Into your document. Note that this does not guarantee that your processing
Or display system will be able to do anything useful with the character.
Example,& # X236E;Wocould insert, in the words of
Unicode standard, an "APL Functional Symbol Semicolon Underbar ".
Whether or not you can print that character is an entirely different issue.
Character references differ from other entity references in a subtle
But significant way. They are expanded immediately by the parser. Using'& #34 ;'Is exactly the same'"'. In particle,
This means you can't use the character reference in an attribute value
Escape the quotation characters.
External Entities
External entities offer a mechanic for dividing your document up
Logical chunks. Rather than authoring a monolithic document, a book with 10
Chapters for example, you can store each chapter in a separate file and use
External entities to "source in" the 10 chapters.
Because external entities in different documents can refer to the same
Files on your file system, external entities provide an opportunity to implement
Reuse. Reuse of small, discrete components (figures, legal boilerplate, warning
Messages) is fairly easy to manage. Implementing reuse on a large scale requires
An entity management system which XML, by itself, does not provide.
A few notes about external entities:
External entities do not have to consist of a single element;
You can make a sequence of three paragraphs, or even a bunch of character
Data with embedded inline markup into an external entity. But the tags in
An external entity must be well balanced (you can't start a tag in an entity
And end it in your document or in another entity ).
External entities can reference internal or other external
Entities, but you cannot have circular references.
You can refer to the same external entity several times in
A single document. Note, however, that if you do this, you will have to avoid
Using ID attributes in the external entity if you're concerned about validity.
Using an external entity which contains an ID in more than one location in
Your document will produce a document that has multiple, duplicate IDs which
Is a validity error.
It is legal to have several external entities that all refer
To the same external file.
There are no additional restrictions placed on the character
Encodings used by external entities. In particle, external entities
Differing encodings can be used in the same document.
External entities, like internal entities, have names and are referenced
In the same manner, although they are declared differently.
Parameter Entities
Internal and external entity references are not expanded in the DTD
Or the internal subset (this allows you to use entity references in the replacement
Text of other entities without concern about the order of declarations). If
You want to have the effect of entities and entity references in your DTD,
Parameter entities must be used. Parameter entity references use"%"Character instead of"&".
Parameter entities can't be used in the content of your document; they simply
Aren't recognized.
It is legal to have a parameter entity and an internal or external entity
With the same name. They are completely different types of entities and cannot
Conflict with each other.
One common use of parameter entities is in conditional sections. Conditional
Sections are a mechanic for parameterizing the DTD. Note, however, that you
Cannot use conditional sections in the internal subset of XML documents.