XML Element Declaration Syntax in DTD

Declaring elements within a Document Type Definition (DTD) defines the valid structure, rules, and allowed content for elements in an XML document. This article breaks down the exact syntax required for DTD element declarations, covering basic content types, child element sequences, mixed content, and occurrence frequency operators.

Basic Syntax

In a DTD, an element is declared using the <!ELEMENT> tag followed by the element name and its content definition:

<!ELEMENT element-name content-type>

The content-type specifies what the element can contain (such as text, other elements, or nothing).


Content Types and Models

1. Parsed Character Data (#PCDATA)

If an element should only contain plain text and no child elements, use #PCDATA (Parsed Character Data).

<!ELEMENT title (#PCDATA)>

2. Empty Elements (EMPTY)

For elements that do not contain any text or child elements (often used alongside attributes), use the EMPTY keyword.

<!ELEMENT br EMPTY>

3. Any Content (ANY)

The ANY keyword allows an element to contain any combination of parsable data and declared elements.

<!ELEMENT description ANY>

4. Child Elements (Sequences)

To declare that an element must contain specific child elements in a strict order, list them inside parentheses separated by commas.

<!ELEMENT note (to, from, heading, body)>

5. Alternative Elements (Choices)

To allow one element or another to appear, separate the child element names with a pipe (|).

<!ELEMENT payment (cash | credit)>

Occurrence Operators

You can control how many times a child element can appear by appending operators to the element name inside the declaration:


Mixed Content

To allow an element to contain both character data and child elements in any order, use a mixed content declaration. The declaration must begin with #PCDATA, list elements separated by |, and end with an asterisk *.

<!ELEMENT paragraph (#PCDATA | bold | italic)*>