XML DTD Entities
An entity in XML is a shortcut — a name that stands for a piece of text or data. Instead of typing the same long text repeatedly, you define it once as an entity and use the entity name wherever the text is needed. The XML parser replaces the entity name with the actual content when it processes the file.
Think of an entity like a text replacement shortcut on your phone. You type "addr" and the phone expands it to your full address. XML entities work exactly the same way.
Types of XML Entities
1. Built-In Predefined Entities
XML defines five entities by default. These represent characters that would otherwise confuse the XML parser:
| Entity | Character | Meaning |
|---|---|---|
| < | < | Less than sign |
| > | > | Greater than sign |
| & | & | Ampersand |
| " | " | Double quote |
| ' | ' | Apostrophe |
These always work in any XML file without any declaration needed.
2. Internal General Entities
You define your own custom text shortcuts in the DTD. Use them in XML content by writing &entityName;
Declaration in DTD
<!ENTITY companyName "Sunrise Technologies Pvt. Ltd."> <!ENTITY supportEmail "support@sunrise.com"> <!ENTITY copyrightYear "2024">
Usage in XML
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE company [ <!ENTITY companyName "Sunrise Technologies Pvt. Ltd."> <!ENTITY supportEmail "support@sunrise.com"> ]> <company> <name>&companyName;</name> <footer>Contact &companyName; at &supportEmail;</footer> <legal>All rights reserved by &companyName;</legal> </company>
What the parser sees after expanding entities
<name>Sunrise Technologies Pvt. Ltd.</name> <footer>Contact Sunrise Technologies Pvt. Ltd. at support@sunrise.com</footer> <legal>All rights reserved by Sunrise Technologies Pvt. Ltd.</legal>
3. External General Entities
External entities store their content in a separate file. The DTD points to that file. When the XML parser encounters the entity, it fetches and inserts the file's content.
<!ENTITY disclaimer SYSTEM "disclaimer.txt">
Usage
<legal>&disclaimer;</legal>
The parser reads disclaimer.txt and inserts its content inside the <legal> element. This is useful for standard legal text or boilerplate content shared across many XML files.
4. Parameter Entities
Parameter entities work only inside the DTD itself — not in XML content. They let you reuse common DTD fragments. They start with a percent sign % in the declaration and %entityName; in use.
Declaration
<!ENTITY % personFields "(name, email, phone)">
Usage inside the DTD
<!ELEMENT customer %personFields;> <!ELEMENT employee %personFields;>
Without the parameter entity, you would repeat (name, email, phone) in every element declaration. The parameter entity defines it once and reuses it everywhere.
Entity Resolution Diagram
XML Source (before parsing):
<footer>Contact &companyName; for help.</footer>
XML Parser reads file
|
v
Finds &companyName;
|
v
Looks up entity in DTD:
"Sunrise Technologies Pvt. Ltd."
|
v
Replaces entity reference with value:
XML Result (after parsing):
<footer>Contact Sunrise Technologies Pvt. Ltd. for help.</footer>
PUBLIC vs SYSTEM in External Entities
External entities use SYSTEM or PUBLIC to locate the external file.
SYSTEM
Points to a file using a local file path or URL.
<!ENTITY terms SYSTEM "terms-of-service.txt"> <!ENTITY logo SYSTEM "http://example.com/logo.txt">
PUBLIC
Points to a publicly registered identifier plus a fallback URL.
<!ENTITY terms PUBLIC "-//Company//Terms//EN" "terms.txt">
Character Entities (Numeric)
You can reference any Unicode character using its numeric code. This avoids encoding problems when special characters appear in XML.
| Format | Example | Character |
|---|---|---|
| Decimal | © | © (copyright) |
| Decimal | € | € (euro) |
| Hexadecimal | © | © (copyright) |
| Hexadecimal | € | € (euro) |
Example in XML
<price>€29.99</price> <copyright>© 2024 Sunrise Tech</copyright>
Complete Working Example
A product catalog that uses entities for repeated content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (product+)>
<!ELEMENT product (name, price, warranty)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT warranty (#PCDATA)>
<!ENTITY warrantyText "1 year manufacturer warranty included.">
<!ENTITY currency "₹">
]>
<catalog>
<product>
<name>Bluetooth Speaker</name>
<price>¤cy;1499</price>
<warranty>&warrantyText;</warranty>
</product>
<product>
<name>USB Hub</name>
<price>¤cy;799</price>
<warranty>&warrantyText;</warranty>
</product>
</catalog>
Key Points to Remember
- Entities are shortcuts that expand to text when the parser processes the file.
- Five predefined entities handle special characters: < > & " '
- Internal general entities are defined in the DTD and used in XML content.
- External entities point to separate files and insert their content at the entity reference.
- Parameter entities work only inside DTD declarations, not in XML content.
- Numeric character references use &#number; to insert any Unicode character.
