DTD - DTD Content Models: EMPTY, ANY, and #PCDATA Explained

A Document Type Definition (DTD) defines the structure and rules of an XML document. One of the most important parts of a DTD is the element content model, which specifies what an element is allowed to contain. XML 1.0 defines EMPTY, ANY, mixed content, and child-element content as possible content specifications. (W3C)

Among these, EMPTY, ANY, and #PCDATA are particularly useful for understanding how DTD controls the contents of XML elements. They represent three different levels of restriction: no content, unrestricted declared content, and text content.

1. What Is a Content Model?

A content model is a rule written inside an declaration that tells an XML parser what can appear between an element's opening and closing tags.

The general syntax is:

For example:

This declaration says that the name element is allowed to contain parsed character data, normally text.

The content model is important because an XML document can be well-formed but still fail DTD validation if an element contains something that its DTD declaration does not permit. The XML specification defines validity rules based on the declared content model. (W3C)


2. EMPTY Content Model

The EMPTY content model is used when an element must contain nothing.

Syntax:

For example:

This declaration states that the image element cannot contain text, child elements, entity references, or whitespace content. The XML specification explicitly defines an element declared as EMPTY as having no content. (W3C)

A valid XML example would be:

 

It can also be written using separate start and end tags:

 

However, the element must remain completely empty.

Invalid example

photo.jpg

This is invalid according to the DTD because photo.jpg is character data and the image element has been declared EMPTY.

Another invalid example is:


Even whitespace between the tags is not permitted for an EMPTY element. (Flylib)

When is EMPTY useful?

EMPTY is useful when an element represents an action, marker, flag, or object whose information is supplied through attributes rather than element content.

For example:


An XML document could contain:

 

The br element itself contains no text or child elements. Its information is represented through its attribute.


3. ANY Content Model

The ANY content model is much less restrictive than EMPTY.

Syntax:

For example:

This tells the DTD that the element can contain character data and declared child elements without imposing a specific sequence or structure on those contents. The XML specification describes ANY as permitting character data and child elements whose types have been declared. (W3C)

For example:



The following can be valid:


    Welcome to XML.

It can also contain declared child elements:


    
    XML is used to represent structured information.

The major difference is that the DTD does not specify a strict order such as:

(title, paragraph)

or restrict the element to text only.

Why is ANY less restrictive?

Suppose we have:

The DTD does not require a specific content structure for catalog.

Compare this with:

The second declaration requires title followed by book, whereas ANY does not impose such a specific content model.

When should ANY be used?

ANY can be useful during XML development or when the structure of an element intentionally needs to remain flexible. However, it provides considerably less validation than a specific content model.

For example, a highly structured XML document might use:

instead of:

The first declaration helps detect structural mistakes. The second provides very little structural restriction.

Therefore, ANY should generally be used only when such flexibility is actually required.


4. #PCDATA Content Model

#PCDATA stands for Parsed Character Data.

It is used when an element is intended to contain text rather than child elements.

Syntax:

For example:

This means that title can contain text.

A valid XML document would be:

 

Another example:

XML:


    XML is a markup language used to represent structured data.

The element contains textual data, but it does not allow child elements under this particular declaration.

For example:


    XML

would not conform to:

because important is a child element rather than character data.


5. Why Is It Called Parsed Character Data?

The term #PCDATA is important because XML processors parse the character data rather than treating it as completely uninterpreted text.

For example:

Hello & welcome

Here:

&

is an entity reference that is processed by the XML parser.

The #PCDATA content model is therefore different from simply thinking of the content as arbitrary raw text.

Also, #PCDATA should not be confused with CDATA as an attribute type. In DTDs, #PCDATA is used in an element's content model, whereas CDATA is commonly used as an attribute type. DTD attribute declarations have their own syntax and type system. (W3C)


6. Difference Between EMPTY, ANY, and #PCDATA

The three can be compared as follows:

Content Model Allows Text Allows Child Elements Level of Restriction
EMPTY No No Very strict
#PCDATA Yes No Strict
ANY Yes Yes, for declared element types Very flexible

Consider the following declarations:



They impose three different rules.

For a:

 

Only empty content is permitted.

For b:

Hello XML

Text is permitted, but child elements are not.

For c:


    Hello XML
    Example

Text and declared child elements can be accommodated.


7. Practical Example

Consider a simple product XML document.

DTD:




XML:


    101
    Laptop
    

Here:

allows the product element to have flexible content.

requires productId to contain text.

requires productName to contain text.

requires image to contain nothing.

This demonstrates how different content models can be combined within the same DTD.


8. EMPTY vs #PCDATA

A common point of confusion is the difference between EMPTY and #PCDATA.

Consider:

The following is invalid:

John

because name cannot contain anything.

Now consider:

The following is valid:

John

The difference is simple:

EMPTY means no content.

#PCDATA means text content is allowed.


9. #PCDATA vs ANY

These two are also significantly different.

Consider:

This allows:

This is a product.

But not:


    Product

Now consider:

The content model is much more flexible, allowing character data and declared child elements.

Therefore:

#PCDATA = text-oriented content
ANY     = flexible content

The W3C XML specification defines these as different content specifications and applies different validity rules to each. (W3C)


10. Important Point About Empty XML Elements

An element declared as EMPTY does not have to use the self-closing syntax.

For example:

 

and:

 

both represent an element with no content.

The DTD declaration:

controls the content, not the particular tag-writing style.

However, this is invalid:

 

because there is whitespace content between the tags. (Flylib)


11. Summary

DTD content models determine what can appear inside XML elements.

EMPTY is used when an element must contain absolutely nothing:

#PCDATA is used when an element should contain parsed character data:

ANY provides a much more flexible content model that permits character data and declared child elements:

The key distinction is:

EMPTY   → no content
#PCDATA → text content
ANY     → flexible text and element content

Understanding these three content models is essential before moving on to more complex DTD declarations involving sequences, choices, mixed content, and occurrence indicators. The XML specification defines these content specifications as part of the declaration mechanism. (W3C)