What Is NDATA in XML DTD and How Does It Work?
In XML Document Type Definitions (DTDs), the NDATA
keyword declares unparsed external entities containing non-XML content
such as images, multimedia, or binary files. This article explains the
technical purpose of NDATA, how it binds external resources
to specific data notation types, and the method by which XML documents
reference these non-XML entities using dedicated attributes.
What Is an Unparsed Entity?
In XML, entities are essentially variables used to represent content. An unparsed entity is an external resource containing data that the XML processor should not parse as XML text. Examples include binary files like JPEG images, PDF documents, or proprietary media formats. Because the XML processor cannot interpret these formats natively, the DTD must inform the parser to pass the resource directly to the target application without parsing its internal contents.
The Role of the NDATA Keyword
The NDATA (Notation Data) keyword acts as a marker
within an external entity declaration. It explicitly tells the XML
parser two things:
- The entity points to an unparsed, non-XML resource.
- The entity’s format corresponds to a predefined helper format, known
as a
NOTATION.
When an XML processor encounters an NDATA declaration,
it validates the entity’s existence and its associated notation, but it
does not attempt to read or validate the entity’s file contents as XML
markup.
Defining Notations and NDATA Entities
To use an unparsed entity with NDATA, you must configure
two separate declarations inside the DTD: a NOTATION
declaration and an ENTITY declaration.
1. The NOTATION Declaration
The notation identifies the format or application associated with the non-XML data, often using a MIME type or a system identifier:
<!NOTATION png SYSTEM "image/png">2. The ENTITY Declaration with NDATA
The entity is declared using the SYSTEM or
PUBLIC identifier to locate the external file, followed by
the NDATA keyword and the notation name:
<!ENTITY company-logo SYSTEM "assets/logo.png" NDATA png>In this example, company-logo is the entity name,
assets/logo.png is the path to the non-XML file, and
NDATA png links the entity to the png notation
defined earlier.
Referencing Unparsed Entities in XML
Unlike parsed entities, which are referenced inline using an
ampersand and semicolon (such as &company-logo;),
unparsed entities cannot appear directly within XML element character
data. Doing so causes an XML parsing error.
Instead, unparsed entities are referenced via attributes explicitly
declared with the ENTITY or ENTITIES type in
the DTD.
DTD Attribute Declaration:
<!ELEMENT graphic EMPTY>
<!ATTLIST graphic source ENTITY #REQUIRED>XML Document Usage:
<graphic source="company-logo"/>Application Processing Workflow
When an XML parser processes the document:
- It reads the attribute value
(
source="company-logo"). - It matches the value against the declared
NDATAentity (company-logo). - It retrieves the system identifier (
assets/logo.png) and notation definition (image/png). - It supplies these metadata values directly to the consuming software application, allowing the application to load and render the non-XML resource correctly.