DTD Entities

Estimated study time: 10 minutes.

An entity in a DTD is a shortcut — a name that stands in for a piece of text or content, so you don't have to repeat it throughout a document.

Built-in Entities

XML reserves a few characters for its own syntax, so they must be written as entities instead of literally:

  • &lt; for <
  • &gt; for >
  • &amp; for &
  • &quot; for "
  • &apos; for '

General Entities

Custom entities you define yourself, useful for repeated text like a company name or a long boilerplate string.

<!DOCTYPE course [
  <!ENTITY provider "Uncodemy">
]>
<course>
  <title>Data Analytics by &provider;</title>
</course>

Every occurrence of &provider; is replaced with Uncodemy when the document is parsed.

External General Entities

<!ENTITY logo SYSTEM "logo.txt">

Pulls content in from an external file rather than defining it inline.

Parameter Entities

Used only within the DTD itself (not the document body) to reuse pieces of the DTD definition, declared with a % sign.

<!ENTITY % text "(#PCDATA)">
<!ELEMENT title %text;>
💡 Tip: Use general entities to avoid repeating long, unchanging strings across a document — if the value ever changes, you only update it in one place.

Character References for Any Symbol

Besides the five built-in entities, XML lets you write any Unicode character by its code point. These are called character references and need no DTD declaration.

<price>&#8377;499</price>       <!-- decimal: rupee sign -->
<note>&#xA9; All rights reserved</note>  <!-- hexadecimal: copyright sign -->

Names like &nbsp; or &copy; work in HTML but are not predefined in XML. If you want them, you must declare them in a DTD yourself, for example <!ENTITY copy "&#xA9;">, or simply use the character reference.

How the Parser Expands Entities

  1. The parser reads the DOCTYPE and records every entity declaration.
  2. While reading the document body, each &name; is looked up.
  3. The replacement text is inserted and parsed as if it had been typed in place, so it may itself contain markup.
  4. An undeclared entity name causes a well-formedness error, and the document is rejected.

Because replacement text is parsed, an entity can hold a fragment of elements:

<!DOCTYPE letter [
  <!ENTITY signature "<sign><name>Support Team</name><mail>help@example.com</mail></sign>">
]>
<letter>
  <body>Your request has been resolved.</body>
  &signature;
</letter>

Unparsed Entities and Notations

An unparsed entity points to non-XML data, such as an image, and tells the application its format through a notation. The parser does not read the file; it only passes the reference to the application.

<!NOTATION png SYSTEM "image/png">
<!ENTITY banner SYSTEM "banner.png" NDATA png>
<!ATTLIST page cover ENTITY #REQUIRED>

<page cover="banner"/>

Unparsed entities are used through attributes of type ENTITY, never with the &name; syntax. They are rare in modern XML, where a plain URL attribute is usually preferred.

Handling Entities Safely

External entities let a document pull in files or URLs at parse time. If an application parses XML from untrusted users with this feature switched on, it can expose local files or be overloaded by deeply nested entity definitions. These problems are known as XXE and entity expansion attacks, and the defence is configuration.

  • Disable DTD processing entirely when your format does not need it.
  • If DTDs are required, turn off external entity resolution and limit entity expansion.
  • In Python, parse untrusted input with the defusedxml package.
  • In Java, set secure features on the parser factory, as shown below.
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
f.setXIncludeAware(false);
f.setExpandEntityReferences(false);

Practical Rules

  • Use general entities for repeated text in documents you control.
  • Use parameter entities to keep large DTDs tidy.
  • Prefer character references over custom names for single symbols.
  • Treat any XML from outside your system as untrusted and parse it with entities restricted.
  • Keep entity names short and meaningful, and document what each one expands to at the top of the DTD.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →