How to Use XML DTD Occurrence Operators

In an XML Document Type Definition (DTD), child element occurrence operators dictate how many times a specific child element can appear within its parent element. By default, an element declared inside a parent must appear exactly once. To modify this strict frequency rule, DTD provides three primary occurrence indicators: the question mark (?), the asterisk (*), and the plus sign (+). Understanding how to apply these operators allows you to create flexible, valid schema definitions for structured data.

The Default Behavior (No Operator)

When a child element is listed inside an <!ELEMENT> declaration without any occurrence symbol, it is mandatory and can appear only once.

<!ELEMENT book (title, author)>

In this example, every <book> element must contain exactly one <title> followed by exactly one <author>.

The Question Mark (?): Zero or One Time

The question mark indicates that an element is optional. It can appear either zero times or exactly one time.

<!ELEMENT person (name, nickname?)>

The Asterisk (*): Zero or More Times

The asterisk allows an element to appear any number of times, including not at all. It represents an optional, repeatable element.

<!ELEMENT library (book*)>

The Plus Sign (+): One or More Times

The plus sign requires the element to appear at least once, but it also allows the element to repeat indefinitely.

<!ELEMENT chapter (title, paragraph+)>

Using Operators with Groups and Choices

Occurrence operators can also be applied to grouped element lists using parentheses to control sequences or choices.

<!ELEMENT document (header, (section | note)+, footer?)>