DTD - Parameter Entities – Creating Reusable DTD Components
1. Introduction
In large XML projects, DTDs can become repetitive. Many content models, attribute lists, or entity definitions are reused across multiple elements. To reduce redundancy and improve maintainability, DTDs provide parameter entities — a mechanism for reusable components.
Parameter entities act like macros: they are declared once and then referenced wherever needed.
2. What are Parameter Entities?
-
A general entity (e.g.,
&name;) is used in XML documents. -
A parameter entity (e.g.,
%name;) is used inside the DTD itself.
Syntax:
<!ENTITY % entityName "replacementText">
To use the entity:
%entityName;
3. Example – Reusing Common Attributes
Without parameter entities:
<!ATTLIST book id ID #REQUIRED>
<!ATTLIST article id ID #REQUIRED>
With parameter entities:
<!ENTITY % commonAttrs "id ID #REQUIRED">
<!ATTLIST book %commonAttrs;>
<!ATTLIST article %commonAttrs;>
This way, if you change the definition of id, you only update it once.
4. Example – Reusing Element Content Models
Defining a reusable group:
<!ENTITY % inline "em | strong | link">
<!ELEMENT para (#PCDATA | %inline;)*>
<!ELEMENT note (#PCDATA | %inline;)*>
Now both <para> and <note> can contain the same inline markup elements.
5. External Parameter Entities
Parameter entities can also pull in external DTD fragments, allowing modular design.
Example:
<!ENTITY % common SYSTEM "common.dtd">
%common;
This technique is used in modular DTDs (e.g., DocBook, XHTML modularization).
6. Conditional Sections with Parameter Entities
Parameter entities can be combined with conditional sections for configuration.
Example:
<!ENTITY % useImages "INCLUDE">
<![ %useImages; [
<!ELEMENT image EMPTY>
<!ATTLIST image src CDATA #REQUIRED>
]]>
-
If
%useImages;isINCLUDE, theimageelement is available. -
If
%useImages;isIGNORE, theimageelement is skipped.
7. Best Practices
-
Use parameter entities for common attributes (like IDs, classes, metadata).
-
Group inline content models with parameter entities.
-
Keep entity names descriptive (
%inline;,%commonAttrs;). -
Store reusable parameter entities in external DTD modules.
-
Avoid overusing them for trivial cases — too many entities can reduce readability.
8. Conclusion
Parameter entities make DTDs modular, maintainable, and reusable. They allow repeating patterns (attributes, content models, modules) to be declared once and reused many times. This reduces duplication and ensures consistency across large XML projects.