DTD Entities
DTD Entities
In XML, entities are variables used to define shorcuts to standard text or special
characters. In XML an entity is made up of three parts - an ampersand (&), an entity
name, and a semicolon (;). The general format of an entity is shown below:
<!ENTITY entity-name "entity-value">
Listing 2-8. Formatting entities.
entity-name refers to the name of the entitiy and
entity-value is the value for the defined entity.
Instead of typing the same text over and over again, you can define an 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 example below shows how to define
and use an entity.
<?xml version="1.0"?>
XML File
<collegename>&college;</collegename>
DTD File
<!ENTITY college "Middle Georgia State University">
Listing 2-9. Defining entities.
In the example, an XML contains the element <collegename>
.
The element includes the user-defined entity &college;
. In the
DTD file, the internal entity college is defined as "Middle Georgia
State University". The user-defined entity can now be used anywhere in the XML file
to define the college name. It is not necessary to re-type "Middle Georgia State
University" each time. When the XML is viewed in the browser, the entity is parsed
and displayed as
<collegename>Middle Georgia State University</collegename>
.
The following table contains a list of pre-defined entities that can be used in
XML without being declared in the DTD.
Pre-defined 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 (") |