XML DTD EMPTY Keyword Explained
In XML Document Type Definitions (DTD), element declarations define
the rules and structure for tags within an XML document. This article
explains the meaning, syntax, and practical implementation of the
EMPTY keyword in an XML DTD element declaration, detailing
how it enforces elements without child nodes or text content while still
permitting attributes.
Meaning of the EMPTY Keyword
In an XML DTD, the EMPTY keyword signifies that the
declared element must not contain any content. This means the element
cannot enclose parsed character data (text), whitespace, or child
elements. It strictly restricts the element to being an empty tag.
Syntax and Declaration
To declare an empty element in a DTD, use the following syntax:
<!ELEMENT element_name EMPTY>For example, to declare an element named line-break:
<!ELEMENT line-break EMPTY>Valid XML Usage
An element declared with the EMPTY keyword can be
represented in an XML document in one of two valid formats:
Self-closing tag:
<line-break />Start and end tags with no content between them:
<line-break></line-break>
Attributes with EMPTY Elements
While an EMPTY element cannot contain text or nested
tags, it can still carry attributes. Attributes are declared separately
using the ATTLIST declaration.
For example, an empty image element carrying source and
dimension data can be defined as follows:
<!ELEMENT image EMPTY>
<!ATTLIST image
src CDATA #REQUIRED
width CDATA #IMPLIED
height CDATA #IMPLIED>In the corresponding XML document, the element is valid when populated with attributes:
<image src="logo.png" width="200" height="100" />Validation Constraints
When a validating XML parser encounters an element declared as
EMPTY, any enclosed characters—including spaces, tabs, or
newlines—will trigger a validation error. The parser strictly requires
the content model of that element to remain entirely void of data.