XML DTD General Entities and How to Reference Them
General entities in an XML Document Type Definition (DTD) act as reusable variables or shortcuts used to represent text, special characters, or external content within the body of an XML document. This article explains what general entities are, how they are declared within an internal or external DTD, the difference between internal and external general entities, and the exact syntax required to reference them inside XML content.
What Are General Entities?
In an XML DTD, an entity is a shortcut that represents a piece of data. General entities are designed specifically for use within the XML document content itself, rather than within the DTD markup (which uses parameter entities). When an XML parser encounters a general entity reference, it replaces the reference with the entity’s defined replacement text or the contents of an external resource.
General entities serve several key purposes: * Code Reusability: Defining frequently used text (such as company names, copyright notices, or addresses) in one central place. * Maintainability: Updating a single entity declaration updates the text across the entire document. * Special Characters: Inserting characters that are difficult to type or have special XML meanings.
How to Declare General Entities
General entities are declared inside the DTD using the
<!ENTITY> declaration. There are two primary types of
general entities: internal and external.
1. Internal General Entities
Internal entities define the replacement text directly inside the DTD declaration.
Syntax:
<!ENTITY entity_name "replacement text">Example:
<!DOCTYPE article [
<!ENTITY author "Jane Doe">
<!ENTITY copyright "Copyright 2025 Example Corp. All rights reserved.">
]>2. External General Entities
External entities point to an external file or resource using a
SYSTEM or PUBLIC identifier. The parser
replaces the reference with the content of the referenced file.
Syntax:
<!ENTITY entity_name SYSTEM "URI">Example:
<!DOCTYPE document [
<!ENTITY legalDisclaimer SYSTEM "http://example.com/disclaimer.xml">
<!ENTITY chapterTwo SYSTEM "chapters/chapter2.xml">
]>How to Reference General Entities
To use a declared general entity inside an XML document, you
reference it using an ampersand (&), the entity name,
and a terminating semicolon (;).
Reference Syntax:
&entity_name;Complete Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article [
<!ELEMENT article (title, content, footer)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT content (#PCDATA)>
<!ELEMENT footer (#PCDATA)>
<!-- General Entity Declarations -->
<!ENTITY authorName "Jane Doe">
<!ENTITY legalNotice "All rights reserved.">
]>
<article>
<title>Understanding XML</title>
<content>This guide was written by &authorName;.</content>
<footer>&legalNotice;</footer>
</article>When parsed, &authorName; expands to
Jane Doe, and &legalNotice; expands to
All rights reserved..
Predefined General Entities
XML includes five predefined general entities that can be referenced without declaring them in a DTD:
<produces<(less than)>produces>(greater than)&produces&(ampersand)'produces'(single quote or apostrophe)"produces"(double quote)