DTD Entities

Reusable content and special characters in XML

🔤 DTD Entities

Entities are shortcuts for frequently used text, special characters, or external content. They help maintain consistency, reduce repetition, and handle special characters safely in XML documents.


<!-- Define entity -->
<!ENTITY company "TechCorp Inc.">

<!-- Use entity -->
<footer>&company;</footer>
                                    

Entity Types

📝

General Entities

Reusable text content in documents

<!ENTITY name "value">
&name;
⚙️

Parameter Entities

Reusable content in DTD itself

<!ENTITY % name "value">
%name;
🔢

Character Entities

Special characters and symbols

<!ENTITY copy "&#169;">
&copy;
📁

External Entities

Content from external files

<!ENTITY name SYSTEM "file.xml">
&name;

🔹 General Entities

General entities are the most common type. They define reusable text that can be referenced throughout your XML document for consistency.

<?xml version="1.0"?>
<!DOCTYPE company [
  <!ELEMENT company (name,address,contact)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT address (#PCDATA)>
  <!ELEMENT contact (#PCDATA)>
  
  <!-- Define entities -->
  <!ENTITY companyName "TechCorp Industries">
  <!ENTITY companySlogan "Innovation for Tomorrow">
  <!ENTITY copyright "Copyright © 2025">
  <!ENTITY phone "+1-555-0123">
]>

<company>
  <name>&companyName;</name>
  <address>&companySlogan; - &copyright;</address>
  <contact>&phone;</contact>
</company>

<!-- Output:
<company>
  <name>TechCorp Industries</name>
  <address>Innovation for Tomorrow - Copyright © 2025</address>
  <contact>+1-555-0123</contact>
</company>
-->

🔹 Built-in Character Entities

XML has five predefined entities for special characters that have meaning in XML syntax. These must be used to display these characters literally.

Entity Character Description
&lt; < Less than
&gt; > Greater than
&amp; & Ampersand
&quot; " Quotation mark
&apos; ' Apostrophe
<message>
  Price: &lt; $100 &amp; &gt; $50
</message>

<quote>
  He said &quot;Hello&quot; and she said &apos;Hi&apos;
</quote>

<!-- Output:
Price: < $100 & > $50
He said "Hello" and she said 'Hi'
-->

🔹 Custom Character Entities

You can define custom entities for special characters using numeric character references. This is useful for symbols and international characters.

<?xml version="1.0"?>
<!DOCTYPE document [
  <!ELEMENT document (#PCDATA)>
  
  <!-- Common symbols -->
  <!ENTITY copy "&#169;">    <!-- © -->
  <!ENTITY reg "&#174;">     <!-- ® -->
  <!ENTITY trade "&#8482;">  <!-- ™ -->
  <!ENTITY euro "&#8364;">   <!-- € -->
  <!ENTITY pound "&#163;">   <!-- £ -->
  <!ENTITY nbsp "&#160;">    <!-- non-breaking space -->
]>

<document>
  &copy; 2024 TechCorp&reg;
  Price: &euro;99 or &pound;85
  Product&trade;
</document>

<!-- Output:
© 2025 TechCorp®
Price: €99 or £85
Product™
-->

🔹 Parameter Entities

Parameter entities are used within the DTD itself to make DTD definitions reusable and maintainable. They help avoid repetition in complex DTDs.

<?xml version="1.0"?>
<!DOCTYPE catalog [
  <!-- Define parameter entity -->
  <!ENTITY % commonAttr "
    id ID #REQUIRED
    created CDATA #IMPLIED
    modified CDATA #IMPLIED">
  
  <!ELEMENT catalog (product+,service+)>
  <!ELEMENT product (name,price)>
  <!ELEMENT service (name,price)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT price (#PCDATA)>
  
  <!-- Use parameter entity -->
  <!ATTLIST product %commonAttr;>
  <!ATTLIST service %commonAttr;>
]>

<catalog>
  <product id="P001" created="2024-01-01">
    <name>Laptop</name>
    <price>999</price>
  </product>
  <service id="S001" created="2024-01-02">
    <name>Support</name>
    <price>99</price>
  </service>
</catalog>

🔹 External Entities

External entities reference content from external files. This is useful for including common content across multiple XML documents or large text blocks.

🔸 External Entity Declaration

<!-- Main XML file -->
<?xml version="1.0"?>
<!DOCTYPE document [
  <!ELEMENT document (header,content,footer)>
  <!ELEMENT header (#PCDATA)>
  <!ELEMENT content (#PCDATA)>
  <!ELEMENT footer (#PCDATA)>
  
  <!-- External entity -->
  <!ENTITY disclaimer SYSTEM "disclaimer.txt">
  <!ENTITY terms SYSTEM "terms.txt">
]>

<document>
  <header>Welcome</header>
  <content>Main content here</content>
  <footer>
    &disclaimer;
    &terms;
  </footer>
</document>

disclaimer.txt:

This information is provided as-is without warranty.

terms.txt:

By using this service, you agree to our terms and conditions.

🔹 Practical Entity Examples

Here's a real-world example showing how entities improve maintainability and consistency in a company website's XML content.

<?xml version="1.0"?>
<!DOCTYPE website [
  <!ELEMENT website (page+)>
  <!ELEMENT page (title,content,footer)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT content (#PCDATA)>
  <!ELEMENT footer (#PCDATA)>
  
  <!-- Company information entities -->
  <!ENTITY companyName "TechCorp Industries">
  <!ENTITY companyEmail "[email protected]">
  <!ENTITY companyPhone "+1-555-0123">
  <!ENTITY companyAddress "123 Tech Street, Silicon Valley, CA">
  
  <!-- Legal entities -->
  <!ENTITY copyright "Copyright &#169; 2024">
  <!ENTITY trademark "&#8482;">
  <!ENTITY registered "&#174;">
  
  <!-- Common text -->
  <!ENTITY contactInfo "Contact us at &companyEmail; or &companyPhone;">
]>

<website>
  <page>
    <title>About &companyName;</title>
    <content>
      Welcome to &companyName;&registered;!
      We are located at &companyAddress;.
      &contactInfo;
    </content>
    <footer>
      &copyright; &companyName;&registered;
      All rights reserved.
    </footer>
  </page>
  
  <page>
    <title>Products</title>
    <content>
      Check out our innovative products&trademark;!
      &contactInfo;
    </content>
    <footer>
      &copyright; &companyName;
    </footer>
  </page>
</website>

🔹 Entity Best Practices

Guidelines:

  • Use for Repetition: Define entities for text that appears multiple times
  • Company Info: Store company name, contact details, copyright
  • Special Characters: Use entities for symbols and international characters
  • Maintainability: Change entity definition once to update everywhere
  • Descriptive Names: Use clear, meaningful entity names
  • External Files: Use external entities for large or shared content
  • Security: Be cautious with external entities (XXE vulnerabilities)

🧠 Test Your Knowledge

How do you reference an entity in XML content?