DTD - EMPTY and ANY Content Models in DTD
In XML Document Type Definition (DTD), a content model specifies what kind of content an element is allowed to contain. Two special content models are EMPTY and ANY. They represent opposite approaches: EMPTY places the strictest restriction on an element, while ANY provides very few restrictions. XML 1.0 formally defines both as valid content specifications for element declarations. (W3C)
1. EMPTY Content Model
The EMPTY content model is used when an XML element must not contain any content. This means the element cannot contain text, child elements, entity references, comments, or processing instructions as its content. (W3C)
The basic syntax is:
<!ELEMENT element-name EMPTY>
For example:
<!ELEMENT br EMPTY>
This declaration tells the XML validator that the br element cannot contain any content.
A corresponding XML document can use the empty-element form:
<br/>
It can also be written as:
<br></br>
Both represent an element with no content. The XML specification notes that an empty-element tag may be used whenever an element happens to have no content, although it is generally recommended for elements declared as EMPTY. (W3C)
2. EMPTY Does Not Mean the Element Cannot Have Attributes
An important point is that declaring an element as EMPTY only restricts its content. It does not prevent the element from having attributes.
For example:
<!ELEMENT image EMPTY>
<!ATTLIST image
src CDATA #REQUIRED
width CDATA #IMPLIED
height CDATA #IMPLIED
>
The XML document could contain:
<image src="photo.jpg" " height="600"/>
The image element contains no child elements or text, but it has three attributes. This is valid because attributes are associated with the element rather than being considered its content. (Flylib)
This makes EMPTY useful for elements that represent standalone information such as markers, references, controls, or objects whose details are supplied entirely through attributes.
3. Example of EMPTY in a Practical DTD
Consider a simple notification document:
<!ELEMENT notification (message, urgent)>
<!ELEMENT message (#PCDATA)>
<!ELEMENT urgent EMPTY>
<!ATTLIST urgent
level CDATA #REQUIRED
>
A valid XML document could be:
<notification>
<message>System maintenance is scheduled tonight.</message>
<urgent level="high"/>
</notification>
Here, urgent cannot contain text or child elements. Its information is represented through the level attribute.
The following would be invalid:
<urgent>Important</urgent>
because the DTD explicitly declares urgent as EMPTY.
Similarly, this would be invalid:
<urgent>
<level>high</level>
</urgent>
because urgent is not allowed to contain a child element.
4. ANY Content Model
The ANY content model is almost the opposite of EMPTY. It allows an element to contain character data and child elements without imposing a specific content structure. (W3C)
The syntax is:
<!ELEMENT element-name ANY>
For example:
<!ELEMENT document ANY>
This tells the validator that the document element can contain a variety of content.
For example:
<document>
This is some text.
<title>Introduction</title>
<section>
<paragraph>Some information.</paragraph>
</section>
</document>
The ANY declaration does not require a particular sequence such as:
title, section, paragraph
Instead, it provides a much more flexible content model.
5. An Important Limitation of ANY
ANY does not mean that completely unknown XML elements can be inserted into the document.
For example:
<!ELEMENT document ANY>
<!ELEMENT title (#PCDATA)>
The following is potentially valid:
<document>
<title>Hello</title>
</document>
because title has been declared.
However, simply declaring document as ANY does not make an undeclared element automatically valid. XML processors still require element types appearing in a valid document to have corresponding declarations. (O'Reilly)
Therefore, ANY provides flexibility in the content arrangement, but it does not turn the DTD into a completely open-ended XML structure.
6. Comparing EMPTY and ANY
| Feature | EMPTY | ANY |
|---|---|---|
| Text content | Not allowed | Allowed |
| Child elements | Not allowed | Allowed |
| Content restriction | Very strict | Very loose |
| Attributes | Allowed | Allowed |
| Main purpose | Define elements with no content | Allow flexible content |
| Validation strictness | Very high | Very low |
The difference can be understood simply:
EMPTY = Nothing inside
ANY = Almost anything inside
7. When Should EMPTY Be Used?
EMPTY is appropriate when an element is intended to act as a marker or standalone element and all required information is represented through attributes.
For example:
<!ELEMENT line-break EMPTY>
XML:
<line-break/>
Another example could be:
<!ELEMENT separator EMPTY>
XML:
<separator/>
These elements do not need textual or child-element content.
Using EMPTY also makes the DTD more precise because the validator knows that content must never appear inside that element.
8. When Should ANY Be Used?
ANY can be useful when the structure of an element is intentionally flexible or is still being developed. Some XML documentation describes ANY as useful during the early design stage of a DTD, when the exact structure has not yet been finalized. (PTAC Triage)
For example:
<!ELEMENT content ANY>
could temporarily allow developers to place different types of declared elements and text inside content.
However, using ANY in a finished DTD is generally discouraged when a precise structure can be defined. A restrictive content model makes validation more meaningful and helps applications understand the expected structure of the XML document. (O'Reilly)
9. EMPTY and ANY in Validation
The difference becomes particularly important during XML validation.
Suppose the DTD contains:
<!ELEMENT emptyElement EMPTY>
<!ELEMENT flexibleElement ANY>
The following is valid:
<emptyElement/>
But this is invalid:
<emptyElement>Hello</emptyElement>
because EMPTY permits no content.
For the ANY element:
<flexibleElement>
Hello
<title>XML</title>
</flexibleElement>
the content is permitted, provided that the elements appearing inside it satisfy the relevant DTD declarations. XML 1.0 specifies that an element declared ANY can contain character data and child elements whose types have been declared. (W3C)
10. Key Points to Remember
EMPTY and ANY are special DTD content specifications used to control how much content an XML element can contain.
EMPTY means the element must have no content at all, although it can still have attributes.
<!ELEMENT element EMPTY>
ANY provides a very flexible content model, allowing character data and declared child elements without specifying a particular sequence or structure.
<!ELEMENT element ANY>
The main difference is therefore the level of restriction. EMPTY provides maximum restriction, while ANY provides maximum flexibility within the limits of the DTD's declared element types. The W3C XML specification explicitly defines both as valid element content specifications. (W3C)
Simple exam definition
EMPTY: A DTD content model that specifies that an element cannot contain any text or child elements.
ANY: A DTD content model that allows an element to contain character data and declared child elements without defining a specific content structure.