Parameter vs General Entities in XML DTD Parsing
In XML Document Type Definitions (DTDs), entities function as variables or macros to facilitate reusability and maintainability. The fundamental difference between a general entity and a parameter entity lies in their scope of use and syntax: general entities are referenced within the XML document content itself, while parameter entities are restricted solely to the DTD for defining and modularizing schema structures.
General Entities
General entities are designed to insert text, special characters, or external content into the body of an XML document. They can also appear within the replacement text of other entities in the DTD.
Declaration Syntax: Defined using the standard
<!ENTITY>declaration.<!ENTITY copyright "Copyright 2024 Example Corp.">Reference Syntax: Referenced using an ampersand (
&) and a semicolon (;).<footer-text>©right;</footer-text>Scope: Expanded during the parsing of the XML document instance.
Types:
- Parsed General Entities: Contain valid XML/text that replaces the entity reference directly.
- Unparsed General Entities: Point to non-XML data (such as images or binaries) via NDATA declarations and are referenced in attributes rather than document text.
Parameter Entities
Parameter entities are used exclusively within the DTD to create reusable components for markup declarations, such as element lists, attribute definitions, and conditional sections. They cannot be expanded within the XML document instance.
Declaration Syntax: Defined with a percent sign (
%) placed between theENTITYkeyword and the entity name.<!ENTITY % common_attrs "id ID #REQUIRED class CDATA #IMPLIED">Reference Syntax: Referenced within the DTD using a percent sign (
%) followed by the entity name and a semicolon (;).<!ATTLIST header %common_attrs;> <!ATTLIST footer %common_attrs;>Scope: Expanded strictly during the parsing of the DTD before or during the validation phase.
Key Differences at a Glance
| Feature | General Entity | Parameter Entity |
|---|---|---|
| Primary Purpose | Content substitution in XML documents | Schema/markup modularity in DTDs |
| Reference Location | XML document body (and DTD values) | Exclusively within DTD declarations |
| Reference Delimiters | &name; |
%name; |
| Declaration Keyword | <!ENTITY name "value"> |
<!ENTITY % name "value"> |
| Parser Handling | Expanded when parsing XML elements | Expanded when compiling the DTD |