ENTITY Declaration
Entities reference data that act as an abbreviation or can be found at an external location. Entities help to reduce the entry of repetitive information and also allow for easier editing (by reducing the number of occurrences of data to edit). There are two types of entity declarations: GENERAL entity declarations, and PARAMETER entity declarations.
The GENERAL ENTITY Declaration:
The types of general entities include:
- INTERNAL (PARSED)
- EXTERNAL (PARSED)
- EXTERNAL (UNPARSED)
INTERNAL (PARSED) GENERAL ENTITY Declaration:
Internal parsed entities generally reference text. The correct definition is that they refer to data that an XML processor has to parse.
<!ENTITY name "entity_value">
|
where:
- entity value: any character that is not an '&', '%' or ' " ', a parameter entity reference ('%Name;'), an entity reference ('&Name;') or a Unicode
character reference.
Example: |
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ENTITY js "Jo Smith">
]>
<author>&js;</author>
|
EXTERNAL (PARSED) GENERAL ENTITY Declaration:
External parsed entities generally reference text. The correct definition is that they refer to data that an XML processor has to parse. External entities are useful for creating a common reference that can be shared between multiple documents. Any changes that are made to external entities are automatically updated in the documents they are referenced. There are two types of external entities: private, and public. Private external entities are identified by the keyword SYSTEM, and are intended for use by a single author or group of authors. Public external entities are identified by the keyword PUBLIC and are intended for broad use.
<!ENTITY name SYSTEM "URI">
|
<!ENTITY name PUBLIC "public_ID" "URI">
|
where:
- URI: In practice, this is a URL where the external parsed entity can be found.
- public_ID: This may be used by an XML processor to generate an alternate URI where the external parsed entity can be found. If it cannot be found at this URI, the XML processor must use the normal URI.
Example: |
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE copyright [
<!ELEMENT copyright (#PCDATA)>
<!ENTITY c SYSTEM "http://www.xmlwriter.net/copyright.xml">
]>
<copyright>&c;</copyright>
|
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE copyright [
<!ELEMENT copyright (#PCDATA)>
<!ENTITY c PUBLIC "-//W3C//TEXT copyright//EN"
"http://www.w3.org/xmlspec/copyright.xml">
]>
<copyright>&c;</copyright>
|
EXTERNAL (UNPARSED) GENERAL ENTITY Declaration:
External unparsed entities generally reference non-XML data. The 100% correct definition is that they refer to data that an XML processor does not have to parse.
<!ENTITY name SYSTEM "URI" NDATA name>
|
<!ENTITY name PUBLIC "public_ID" "URI" NDATA name>
|
Example: |
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE img [
<!ELEMENT img EMPTY>
<!ATTLIST img src ENTITY #REQUIRED>
<!ENTITY logo SYSTEM "http://www.xmlwriter.net/logo.gif" NDATA gif>
<!NOTATION gif PUBLIC "gif viewer">
]>
<img src="logo"/>
|
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE img [
<!ELEMENT img EMPTY>
<!ATTLIST img src ENTITY #REQUIRED>
<!ENTITY logo PUBLIC "-//W3C//GIF logo//EN"
"http://www.w3.org/logo.gif" NDATA gif>
<!NOTATION gif PUBLIC "gif viewer">
]>
<img src="logo"/>
|
Using Entities Within Entities:
The following examples show how general entities can be used in the DTD.
CORRECT Example: |
<?xml version="1.0"?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ENTITY email "josmith@theworldaccordingtojosmith.com">
<!ENTITY js "Jo Smith &email;">
]>
<author>&js;</author>
|
INCORRECT Example: |
<!ENTITY email "user@user.com &js;">
<!ENTITY js "Jo Smith &email;">
|
Predefined GENERAL Entities:
Predefined entities are entities already used for markup. The table below lists the predefined entities and how to declare them in a DTD.
Predefined Entities: |
How to Declare These Entities in a DTD: |
< |
<!ENTITY lt "&#60;"> |
> |
<!ENTITY gt ">"> |
& |
<!ENTITY amp "&#38;"> |
' |
<!ENTITY apos "'"> |
" |
<!ENTITY quot """> |
The PARAMETER ENTITY Declaration:
The types of parameter entities include:
- INTERNAL (PARSED)
- EXTERNAL (PARSED)
Rules:
- Parameter entity references may not be used within markup in an internal DTD
.
INTERNAL (PARSED) PARAMETER ENTITY Declaration:
Internal parameter entity references are used to declare entities existing only in the DTD.
<!ENTITY % name "entity_value">
|
where:
- entity_value: any character that is not an '&', '%' or ' " ', a parameter entity reference ('%Name;'), an entity reference ('&Name;') or a Unicode
character reference.
Examples: |
<!ENTITY % p "(#PCDATA)">
<!ELEMENT student (id,surname,firstname,dob,(subject)*)>
<!ELEMENT id %p;>
<!ELEMENT surname %p;>
<!ELEMENT firstname %p;>
<!ELEMENT dob %p;>
<!ELEMENT subject %p;>
|
<!ELEMENT author (#PCDATA)>
<!ENTITY % js "Jo Smith">
<!ENTITY wb "written by %js;">
|
<!ENTITY % info "(id,surname,firstname)">
<!ELEMENT lab_group_A %info;>
<!ELEMENT lab_group_B %info;>
<!ELEMENT lab_group_C %info;>
|
Note:
- Note the use of external DTD examples above. Parameter entity references may not be used within markup in an internal DTD
.
EXTERNAL (PARSED) PARAMETER ENTITY Declaration:
External parameter entity references are used to link external DTDs. There are two types of external entities: private, and public. Private external entities are identified by the keyword SYSTEM, and are intended for use by a single author or group of authors. Public external entities are identified by the keyword PUBLIC and are intended for broad use.
<!ENTITY % name SYSTEM "URI">
%name;
|
<!ENTITY % name PUBLIC "public_ID" "URI">
%name;
|
where:
- URI: In practice, this is a URL where the external parameter entity can be found.
- public_ID: This may be used by an XML processor to generate an alternate URI where the external parameter entity can be found. If it cannot be found at this URI, the XML processor must use the normal URI.
Example: |
<?xml version="1.0" standalone="no"?>
<!DOCTYPE student [
<!ENTITY % student SYSTEM "http://www.university.com/student.dtd">
%student;
]>
|
|