Skip to content

DTD Elements

Within a Document Type Definition (DTD), element declarations define the names of allowed elements and specify what content each element can contain.

Reminder: While DTDs define basic structure, XML Schema (XSD) is the modern, more powerful standard for defining XML constraints, including rich data types.

The basic syntax for declaring an element in a DTD is:

<!ELEMENT elementname contentmodel>

elementname is the name of the XML tag you are defining. contentmodel specifies what kind of content the element is allowed to hold.

Elements that must not contain any content (text or child elements) are declared using the keyword EMPTY.

DTD Declaration:

<!ELEMENT img EMPTY>

Valid XML Example:

<img src="image.png" alt="Logo"/>

Invalid XML Example:

<img>Some Text</img> <!-- Error: img must be empty -->

Note: Empty elements can still have attributes.

Elements designed to hold only parseable character data (text, which may include references to entities) use the #PCDATA keyword within parentheses.

DTD Declaration:

<!ELEMENT title (#PCDATA)>

Valid XML Example:

<title>Learning XML Basics</title>

Invalid XML Example:

<title>Chapter 1 <section>Intro</section></title> <!-- Error: Cannot contain child elements -->

To allow an element to contain any combination of text and any declared child elements (a very loose definition), use the ANY keyword. This is generally discouraged in favor of more specific content models.

DTD Declaration:

<!ELEMENT description ANY>

Valid XML Examples:

<description>Just text.</description>
<description><para>Paragraph</para> and <bold>bold</bold> text.</description>
<description/>

Declaring Elements with Child Elements (Sequences)

Section titled “Declaring Elements with Child Elements (Sequences)”

To specify that an element must contain a specific sequence of child elements, list the child element names within parentheses, separated by commas.

DTD Declaration (assuming to, from, heading, body are also declared):

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

Valid XML Example:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget!</body>
</note>

Invalid XML Examples:

<note><from>Jani</from><to>Tove</to>...</note> <!-- Error: Wrong order -->
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading></note> <!-- Error: Missing body -->

Each child element listed must appear exactly once and in the specified order.

You can control how many times a child element (or a group of elements) must appear using occurrence indicators placed after the element name or group:

  • No indicator: Must occur exactly once (as in the sequence example above).
  • + (Plus sign): Must occur one or more times.
  • * (Asterisk): Can occur zero or more times (optional, repeatable).
  • ? (Question mark): Can occur zero or one time (optional).

Examples:

<!ELEMENT email (to+, from, subject?, cc*, body)>
<!-- Meaning:
- One or more 'to' elements
- Exactly one 'from' element
- Zero or one 'subject' element
- Zero or more 'cc' elements
- Exactly one 'body' element
-->

To allow an element to contain one choice from a list of possibilities, separate the options with a pipe | symbol within parentheses.

DTD Declaration:

<!ELEMENT contact (email | phone | address)>
<!-- Meaning: A contact element must contain exactly one of: an email, a phone, OR an address element -->

You can combine choices with sequences and occurrence indicators using parentheses for grouping:

<!ELEMENT person (name, (email | phone)?, address*)>
<!-- Meaning: name (required), then optionally an email OR a phone, then zero or more addresses -->

Mixed content allows an element to contain both text (#PCDATA) and child elements, in any order and with any number of occurrences. This is often used for document-like structures (e.g., paragraphs with bold or italic text).

The syntax requires #PCDATA to be the first item in a choice list, followed by the allowed child element names, all separated by |, and the entire group followed by *.

DTD Declaration (assuming b and i elements are also defined):

<!ELEMENT paragraph (#PCDATA | b | i)*>

Valid XML Example:

<paragraph>This is **bold** text and this is <i>italic</i>.</paragraph>
<paragraph>Just text.</paragraph>
<paragraph>**Only bold**.</paragraph>

Note: When using mixed content, you lose control over the order and exact number of child elements within the text.