Entity Declarations
May 3, 1999
In the last section, we touched briefly upon the concept of
entities. If you recall, both general and parameter entities
were used like macros or aliases in XML.
Essentially, an entity allows you to create an alias to some
large bit of text. Elsewhere in the document, you can refer
to the large bit of text simply by referring to its alias.
As you can imagine, this saves a lot for time that might
otherwise be spent retyping the same text. It also means
that modifications to data need only happen in one
centralized locale to implement global changes.
General Entities
General entities allow you to create document-wide entities
and look something like:
<!ENTITY NAME "text that you want to be
represented by the entity">
In the real world, you might have something that looked like
the following:
<!ENTITY full_name "Diego Ramirez
Valenzuela Martinez Perez the 5th">
Entities are referenced using an
%ENTITYNAME;
such as
<!ENTITY TAG_NAMES "NAME | EMAIL | PHONE | ADDRESS">
<!ELEMENT BUSINESS_CONTACT (%TAG_NAMES; | COMPANY_NAME)>
Make sure you remember the semi-colon. I forget this all
the time :)
|
NOTE: You can specify an entity that has text defined
external to the document by using the SYSTEM keyword such as:
<!ENTITY license_agreement
SYSTEM "http://www.mydomain.com/license.xml">
In this case, the XML processor will replace the entity
reference with the contents of the document specified.
|
Be careful that when defining entities, that you define
them before using them. Thus, the following would be
invalid because the TAG_NAMES alias is defined after it is used.
<!ELEMENT PERSONAL_CONTACT (%TAG_NAMES; | BIRTHDAY)>
<!ELEMENT BUSINESS_CONTACT (%TAG_NAMES; | COMPANY_NAME)>
<!ENTITY TAG_NAMES "NAME | EMAIL | PHONE | ADDRESS">
Parameter Entities
Parameter entities, that can be either internal or external,
are only used within the DTD and look something like the
following:
<!ENTITY % ALIAS "text to be aliased">
For example, you might have something like....
<!ENTITY % NAME "text that you want to be represented">
Using Parameter entities, you can shorten the declarations of
other elements and attributes such as:
<!ENTITY % TAG_NAMES "NAME | EMAIL | PHONE | ADDRESS">
<!ELEMENT PERSONAL_CONTACT (%TAG_NAMES; | BIRTHDAY)>
<!ELEMENT BUSINESS_CONTACT (%TAG_NAMES; | COMPANY_NAME)>
|
NOTE: Parameter Entity declarations must precede any
reference to them and must be properly nested.
|
Defining Valid Element Attributes
Introduction to XML For Web Developers | Table of Contents
Internal Versus External DTDs
|