What Are Parameter Entities in XML DTD?

Parameter entities in an XML Document Type Definition (DTD) are specialized macros used exclusively to define and reuse DTD code components. Unlike general entities, which insert replacement text into the XML document body, parameter entities are used entirely within the DTD itself to streamline schema maintenance, reduce redundancy, and manage external modular definitions. This article explains what parameter entities are, their syntax, and the specific scenarios where they are applied.

Definition and Syntax

A parameter entity acts as a variable within a DTD. It allows schema authors to define a string of text, a list of attributes, an element content model, or an external file reference once and reuse it across multiple declarations.

Parameter entities are identified by the percent sign (%).

Declaration Syntax

To declare a parameter entity within an internal or external DTD:

<!ENTITY % EntityName "replacement_text">

For external parameter entities pointing to separate DTD files:

<!ENTITY % EntityName SYSTEM "URI_to_file.dtd">

Reference Syntax

To call or expand a parameter entity inside the DTD, prefix the entity name with % and terminate it with ;:

%EntityName;

Where Parameter Entities Are Used

Parameter entities are strictly restricted to the DTD subset and cannot be referenced within the XML document content. Their primary use cases include:

1. Reusable Content Models

When multiple elements share identical or similar child elements, a parameter entity can define the shared structure. This prevents duplicate code and ensures consistency.

<!-- Define a common group of inline elements -->
<!ENTITY % inline "b | i | u | span">

<!-- Use the entity in element declarations -->
<!ENTITY % text_content "(#PCDATA | %inline;)*">

<!ELEMENT p %text_content;>
<!ELEMENT li %text_content;>

2. Common Attribute Lists

If multiple XML elements share a standard set of attributes (such as id, class, or localization tags), a parameter entity can bundle these attributes.

<!-- Define common attributes -->
<!ENTITY % core_attrs
  "id ID #IMPLIED
   class CDATA #IMPLIED"
>

<!-- Apply to multiple elements -->
<!ATTLIST header %core_attrs;>
<!ATTLIST section %core_attrs;>
<!ATTLIST footer %core_attrs;>

3. Modularizing Large DTDs

Parameter entities allow large, complex DTDs to be split into smaller, manageable files. An external parameter entity imports components such as table definitions or mathematical markup modules into the main DTD.

<!-- Import an external DTD module -->
<!ENTITY % table_module SYSTEM "tables.dtd">
%table_module;

4. Managing Conditional Sections

In external DTD subsets, parameter entities frequently control conditional parsing blocks (INCLUDE and IGNORE). This allows developers to toggle specific rules or modules on and off dynamically.

<!ENTITY % draft "INCLUDE">
<!ENTITY % final "IGNORE">

<![%draft;[
  <!ELEMENT comment (#PCDATA)>
]]>

<![%final;[
  <!-- Draft elements are omitted in final production -->
]]>

Summary of Differences: Parameter vs. General Entities

Feature Parameter Entities General Entities
Declaration Marker Contains % (<!ENTITY % name "...">) No % (<!ENTITY name "...">)
Reference Syntax %name; &name;
Scope of Use Only inside the DTD Inside the XML instance document
Primary Purpose DTD modularity, code reuse, and schema maintenance Document content macros and character shortcuts