DTD - ENTITY and ENTITIES Attribute Types in DTD
In XML Document Type Definitions (DTD), ENTITY and ENTITIES are attribute types used to store references to entities. They are particularly useful when an XML document needs to refer to predefined or externally declared entities. Although the terms look similar, they have an important difference: ENTITY allows one unparsed entity reference, whereas ENTITIES allows one or more unparsed entity references separated by spaces.
1. ENTITY Attribute Type
The ENTITY attribute type specifies that an attribute can contain the name of one unparsed entity declared in the DTD.
The general syntax is:
<!ATTLIST element-name
attribute-name ENTITY
>
For example:
<!DOCTYPE document [
<!NOTATION jpg SYSTEM "image/jpeg">
<!ENTITY photo SYSTEM "photo.jpg" NDATA jpg>
<!ELEMENT document EMPTY>
<!ATTLIST document
image ENTITY
>
]>
Here, the entity photo is declared as an unparsed entity using NDATA. The image attribute is declared with the ENTITY type.
An XML document can then contain:
<document image="photo"/>
The value photo refers to the declared unparsed entity.
An important point is that an ENTITY attribute does not contain the actual external file contents. Instead, it contains the name of an entity declaration.
2. What Is an Unparsed Entity?
To understand ENTITY, it is necessary to understand unparsed entities.
A normal parsed entity contains XML or text that the XML processor can process as part of the document. An unparsed entity, on the other hand, refers to external data that is not intended to be parsed as XML.
For example:
<!NOTATION jpg SYSTEM "image/jpeg">
<!ENTITY logo SYSTEM "logo.jpg" NDATA jpg>
The declaration tells the XML processor that logo represents an external JPEG file.
The NDATA keyword indicates that the entity contains non-XML data. The notation jpg identifies the type or format of that external data.
This type of entity can be referenced through an attribute declared as ENTITY.
3. ENTITIES Attribute Type
The ENTITIES attribute type is similar to ENTITY, but it allows an attribute to contain multiple unparsed entity names.
The general syntax is:
<!ATTLIST element-name
attribute-name ENTITIES
>
For example:
<!DOCTYPE gallery [
<!NOTATION jpg SYSTEM "image/jpeg">
<!ENTITY photo1 SYSTEM "photo1.jpg" NDATA jpg>
<!ENTITY photo2 SYSTEM "photo2.jpg" NDATA jpg>
<!ELEMENT gallery EMPTY>
<!ATTLIST gallery
images ENTITIES
>
]>
An XML document could contain:
<gallery images="photo1 photo2"/>
Here, the images attribute contains two entity names:
photo1
photo2
Both entities must have been declared as unparsed entities in the DTD.
4. Difference Between ENTITY and ENTITIES
The primary difference is the number of entity references that can be supplied.
| Attribute Type | Number of Entity Names | Example |
|---|---|---|
ENTITY |
One | image="photo1" |
ENTITIES |
One or more | images="photo1 photo2" |
With ENTITY, the attribute value must identify a single unparsed entity.
With ENTITIES, the attribute value can contain a whitespace-separated list of unparsed entities.
For example:
<document image="photo1"/>
is suitable for an ENTITY attribute.
Whereas:
<gallery images="photo1 photo2 photo3"/>
is suitable for an ENTITIES attribute.
5. Declaration of Entities
Before an ENTITY or ENTITIES attribute can be used, the corresponding unparsed entities need to be declared.
For example:
<!NOTATION png SYSTEM "image/png">
<!ENTITY image1 SYSTEM "image1.png" NDATA png>
<!ENTITY image2 SYSTEM "image2.png" NDATA png>
The notation declaration identifies the type of external data, while the entity declaration associates an entity name with that external resource.
The following declaration:
<!ENTITY image1 SYSTEM "image1.png" NDATA png>
can be understood as follows:
-
ENTITYdeclares an entity. -
image1is the entity name. -
SYSTEM "image1.png"specifies the external resource. -
NDATAidentifies it as an unparsed entity. -
pngspecifies the notation associated with the data.
6. ENTITY Attribute Example
Consider a document that stores information about a product and its product image.
The DTD could be:
<!DOCTYPE product [
<!NOTATION jpg SYSTEM "image/jpeg">
<!ENTITY productImage SYSTEM "product.jpg" NDATA jpg>
<!ELEMENT product EMPTY>
<!ATTLIST product
image ENTITY
>
]>
The XML document can then be:
<product image="productImage"/>
The value productImage is not the image itself. It identifies the unparsed entity declared in the DTD.
This approach can be useful when an XML document needs to maintain references to external non-XML resources.
7. ENTITIES Attribute Example
Suppose a gallery contains several external images.
The DTD could be:
<!DOCTYPE gallery [
<!NOTATION jpg SYSTEM "image/jpeg">
<!ENTITY image1 SYSTEM "image1.jpg" NDATA jpg>
<!ENTITY image2 SYSTEM "image2.jpg" NDATA jpg>
<!ENTITY image3 SYSTEM "image3.jpg" NDATA jpg>
<!ELEMENT gallery EMPTY>
<!ATTLIST gallery
images ENTITIES
>
]>
The XML document could contain:
<gallery images="image1 image2 image3"/>
The images attribute contains three entity names. Because its type is ENTITIES, multiple entity references can be provided.
8. Validation Rules
DTD validation checks whether values assigned to ENTITY and ENTITIES attributes correspond to valid entity declarations.
For an ENTITY attribute:
<product image="productImage"/>
productImage must refer to a declared unparsed entity.
For an ENTITIES attribute:
<gallery images="image1 image2"/>
both image1 and image2 must refer to appropriate declared unparsed entities.
An undeclared entity name can cause the XML document to fail DTD validation.
For example:
<gallery images="image1 unknownImage"/>
If unknownImage has not been declared as an appropriate entity, the document is invalid.
9. ENTITY Versus General Entity References
It is important not to confuse the ENTITY attribute type with ordinary entity references such as:
&name;
These represent different concepts.
For example:
<!ENTITY company "Example Corporation">
is a parsed entity containing text. It can be referenced in XML using:
&company;
An ENTITY attribute, however, stores the name of an unparsed entity:
<!ATTLIST product image ENTITY>
and the XML might contain:
<product image="productImage"/>
The two mechanisms therefore serve different purposes.
10. Advantages of ENTITY and ENTITIES
The ENTITY and ENTITIES attribute types are useful when XML documents need to associate elements with external non-XML resources.
They provide a structured way to identify external resources through entity declarations. They also allow the DTD to impose validation rules on those references. ENTITY is appropriate when only one external resource needs to be associated with an element, while ENTITIES is useful when several external resources can be associated with the same element.
These features were particularly relevant in traditional XML document systems that needed to reference images, multimedia files, binary resources, or other external data.
Conclusion
The ENTITY and ENTITIES attribute types provide a mechanism for representing references to unparsed external entities within XML documents. ENTITY accepts a single entity name, whereas ENTITIES accepts a whitespace-separated list of entity names. These entities must be declared appropriately in the DTD, normally using NDATA together with a notation declaration.
Understanding these attribute types is important for working with advanced DTDs because they demonstrate how XML can maintain structured references to external non-XML data while still allowing the DTD to validate those references.