DTD - Unparsed Entities and NOTATION-Based External Data in DTD

Introduction

In XML, entities are commonly used to represent reusable pieces of data. Most entities contain text that can be directly inserted into an XML document. However, XML also supports a special type called an unparsed entity. An unparsed entity is used to identify external data that is not XML text, such as an image, audio file, video, PDF document, or other binary resource.

Because XML parsers cannot interpret the contents of such binary or non-XML data as XML, an unparsed entity is not expanded into the document like a normal parsed entity. Instead, it provides a way for an XML document to reference and identify external data.

What Is an Unparsed Entity?

An unparsed entity is an external entity whose content is not required to follow XML syntax. It is declared in the DTD using the NDATA keyword.

A basic declaration looks like this:

<!ENTITY logo SYSTEM "logo.jpg" NDATA jpg>

Here:

  • logo is the name of the entity.

  • SYSTEM "logo.jpg" identifies the external resource.

  • NDATA indicates that the entity contains non-XML data.

  • jpg is the notation associated with the external data.

Unlike a normal parsed entity, the XML processor does not attempt to read logo.jpg as XML. The entity simply provides information about where the external data is located and what type of data it represents.

What Is a NOTATION?

A NOTATION declaration tells an XML processor about the format or type of external data referenced by an unparsed entity.

For example:

<!NOTATION jpg SYSTEM "image/jpeg">

This declaration associates the notation name jpg with the MIME type or other identifying information image/jpeg.

The notation itself does not contain the actual image or binary data. It provides a name that describes the format of that external resource.

An unparsed entity can then refer to this notation:

<!ENTITY logo SYSTEM "logo.jpg" NDATA jpg>

The relationship can therefore be understood as:

NOTATION
   |
   | identifies format
   v
jpg
   |
   | associated with
   v
Unparsed Entity
   |
   | points to
   v
logo.jpg

Complete Example

Consider the following DTD:

<!DOCTYPE product [
    <!NOTATION jpg SYSTEM "image/jpeg">
    <!ENTITY productImage SYSTEM "product.jpg" NDATA jpg>

    <!ELEMENT product (name, image)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT image EMPTY>
    <!ATTLIST image
        src ENTITY #REQUIRED
    >
]>

An XML document could use the entity as follows:

<product>
    <name>Digital Camera</name>
    <image src="productImage"/>
</product>

The DTD declares productImage as an unparsed entity. The entity points to product.jpg, while the jpg notation identifies the type of external data.

The XML parser does not attempt to interpret product.jpg as XML.

Parsed Entity vs Unparsed Entity

The main difference between parsed and unparsed entities is how their contents are treated.

Feature Parsed Entity Unparsed Entity
Content XML-compatible text Non-XML data
XML parser reads content Yes No
Can be expanded into XML text Yes No
Uses NDATA No Yes
Requires notation No Yes
Typical use Reusable text Images, binary files, external resources

For example, a parsed entity might be:

<!ENTITY company "ABC Technologies">

It can be referenced within XML content and replaced with its textual value.

An unparsed entity is different:

<!ENTITY photo SYSTEM "photo.jpg" NDATA jpg>

The XML processor does not replace photo with the contents of photo.jpg.

Why Are Unparsed Entities Needed?

XML was designed primarily to represent structured textual information. However, real-world applications often need to associate XML data with external resources.

For example, an XML document describing a product might need to reference:

  • Product images

  • Audio recordings

  • Video files

  • PDF documents

  • CAD files

  • Compressed files

  • Other binary resources

Trying to place binary data directly into ordinary XML content would be inefficient and inappropriate. Unparsed entities provide a mechanism for associating such external resources with XML documents without requiring the XML parser to understand their internal formats.

Role of NDATA

The keyword NDATA is particularly important.

Consider:

<!ENTITY diagram SYSTEM "diagram.png" NDATA png>

The NDATA declaration tells the XML processor that diagram represents unparsed data.

Without NDATA, an external entity would normally be treated as a parsed entity, meaning the processor would expect its content to be XML or text that can participate in XML processing.

With NDATA, the processor knows that the referenced resource is external non-XML data.

Declaring Multiple Notations

A DTD can define multiple notation types.

For example:

<!NOTATION jpg SYSTEM "image/jpeg">
<!NOTATION png SYSTEM "image/png">
<!NOTATION pdf SYSTEM "application/pdf">

Unparsed entities can then refer to different notations:

<!ENTITY photo SYSTEM "photo.jpg" NDATA jpg>
<!ENTITY diagram SYSTEM "diagram.png" NDATA png>
<!ENTITY document SYSTEM "manual.pdf" NDATA pdf>

This allows the DTD to describe different types of external resources.

Important Limitation

An unparsed entity cannot normally be referenced directly as ordinary XML character content.

For example, this is not the normal purpose of an unparsed entity:

<description>&photo;</description>

An unparsed entity is intended to identify external non-XML data rather than provide textual content for an XML element.

Instead, applications commonly associate the entity with an attribute of type ENTITY, allowing the XML document to identify the external resource.

For example:

<!ATTLIST product
    image ENTITY #IMPLIED
>

Then:

<product image="photo"/>

Here, image identifies the unparsed entity named photo.

Practical Applications

Unparsed entities and notations are useful when XML acts as a metadata or catalog format.

For example, a digital library could use XML to describe documents:

<book>
    <title>XML Fundamentals</title>
    <cover image="bookCover"/>
</book>

The DTD could define:

<!NOTATION jpg SYSTEM "image/jpeg">
<!ENTITY bookCover SYSTEM "cover.jpg" NDATA jpg>

The XML contains the structured information about the book, while the actual image remains an external file.

Another application could be a multimedia catalog:

<media>
    <title>Training Video</title>
    <video file="trainingVideo"/>
</media>

The DTD could associate trainingVideo with an external video resource.

Unparsed Entities and XML Processing

It is important to understand that an unparsed entity does not mean that XML itself processes the binary resource.

The XML processor mainly knows:

  1. The name of the entity.

  2. Where the external resource is located.

  3. Which notation describes its format.

  4. That the resource should not be parsed as XML.

An application using the XML document may then use this information to locate and process the external file.

Therefore, unparsed entities provide a bridge between structured XML metadata and external non-XML resources.

Key Points to Remember

An unparsed entity is an external entity containing non-XML data. It is declared using the NDATA keyword and is normally associated with a NOTATION declaration.

A NOTATION identifies the format or type of external data. It does not contain the data itself.

For example:

<!NOTATION png SYSTEM "image/png">
<!ENTITY logo SYSTEM "logo.png" NDATA png>

Here, png identifies the format, while logo identifies the external image.

The most important distinction is that parsed entities contain data that XML can process, whereas unparsed entities identify external data that XML does not parse.

This topic is especially important for understanding how DTDs can connect XML documents with external multimedia and binary resources while keeping the XML document itself structured and text-based.