DTD - DTD Occurrence Indicators: ?, *, and + in Detail

DTD occurrence indicators are symbols used in a Document Type Definition (DTD) to specify how many times an element can appear inside another element. They are especially useful when designing the structure of an XML document because not every child element needs to occur exactly once.

The three main occurrence indicators are:

  • ? — zero or one occurrence

  • * — zero or more occurrences

  • + — one or more occurrences

These indicators are placed immediately after an element name in a DTD content model.

1. The ? Occurrence Indicator

The ? symbol means that an element can appear zero times or one time. In other words, the element is optional, but if it appears, it can occur only once.

Consider the following DTD:

<!ELEMENT student (name, email?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>

Here, name is mandatory because it does not have an occurrence indicator. The email element has ?, so it is optional.

Therefore, both of these XML documents are valid:

<student>
    <name>Rahul</name>
</student>

And:

<student>
    <name>Rahul</name>
    <email>[email protected]</email>
</student>

However, this is invalid:

<student>
    <name>Rahul</name>
    <email>[email protected]</email>
    <email>[email protected]</email>
</student>

The reason is that email? permits the element to occur at most once.

The ? indicator is useful for optional information such as a middle name, email address, phone number, or secondary address.

2. The * Occurrence Indicator

The * symbol means that an element can appear zero or more times. This means the element is completely optional and, if present, it can occur repeatedly.

For example:

<!ELEMENT student (name, phone*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>

In this example, name must occur exactly once, while phone can occur any number of times.

This XML is valid:

<student>
    <name>Rahul</name>
</student>

There are no phone elements, but the document is still valid because phone* allows zero occurrences.

This is also valid:

<student>
    <name>Rahul</name>
    <phone>9876543210</phone>
</student>

And this is valid as well:

<student>
    <name>Rahul</name>
    <phone>9876543210</phone>
    <phone>9123456780</phone>
    <phone>9988776655</phone>
</student>

The * indicator is commonly used when an element may have a variable number of occurrences and there is no requirement that at least one must exist.

For example, a library could contain zero or more books:

<!ELEMENT library (book*)>
<!ELEMENT book (#PCDATA)>

A library with no books is valid, as is a library containing one, ten, or many books.

3. The + Occurrence Indicator

The + symbol means that an element must appear one or more times.

Unlike *, it does not allow zero occurrences.

For example:

<!ELEMENT class (student+)>
<!ELEMENT student (#PCDATA)>

Here, a class must contain at least one student.

This is valid:

<class>
    <student>Rahul</student>
</class>

This is also valid:

<class>
    <student>Rahul</student>
    <student>Anita</student>
    <student>Vijay</student>
</class>

However, the following is invalid:

<class>
</class>

The reason is that student+ requires at least one occurrence of student.

The + indicator is useful when an XML structure must contain at least one item but can contain additional items. Examples include students in a class, books in a collection, products in an order, or employees in a department.

Difference Between ?, *, and +

The easiest way to understand these three indicators is to compare their minimum and maximum occurrences.

Indicator Minimum occurrences Maximum occurrences Meaning
? 0 1 Optional, maximum one
* 0 Unlimited Optional, unlimited
+ 1 Unlimited Mandatory, unlimited

For example:

email?

means:

0 or 1 email

While:

phone*

means:

0 or more phones

And:

student+

means:

1 or more students

Using Occurrence Indicators with Sequences

Occurrence indicators can also be applied to groups of elements.

For example:

<!ELEMENT order (product, quantity)+>

Here, the complete sequence product, quantity must occur one or more times.

A valid XML structure could be:

<order>
    <product>Laptop</product>
    <quantity>1</quantity>
    <product>Mouse</product>
    <quantity>2</quantity>
</order>

The + applies to the entire group (product, quantity) rather than only to quantity.

Similarly:

<!ELEMENT order (product, quantity)*>

allows the complete group to occur zero or more times.

This distinction is important because placing the occurrence indicator in different positions can change the meaning of the DTD.

Combining Different Occurrence Indicators

Occurrence indicators can be used together within a content model.

For example:

<!ELEMENT employee (name, email?, phone*)>

This means:

  • name must occur exactly once.

  • email can occur zero or one time.

  • phone can occur zero or more times.

A valid document could therefore be:

<employee>
    <name>Rahul</name>
</employee>

It could also contain an email:

<employee>
    <name>Rahul</name>
    <email>[email protected]</email>
</employee>

Or multiple phone numbers:

<employee>
    <name>Rahul</name>
    <email>[email protected]</email>
    <phone>9876543210</phone>
    <phone>9123456780</phone>
</employee>

The order is still important because the DTD specifies the sequence:

name → email → phone

Therefore, an XML document that places phone before name would not satisfy this particular content model.

Important Difference Between * and +

The most common mistake when learning occurrence indicators is confusing * and +.

Consider:

<!ELEMENT library (book*)>

This allows:

<library>
</library>

because zero books are permitted.

Now consider:

<!ELEMENT library (book+)>

This does not allow an empty library:

<library>
</library>

At least one book must be present.

Therefore:

* = zero or more
+ = one or more

The difference is simply whether zero occurrences are allowed.

Occurrence Indicators and XML Validation

DTD occurrence indicators are particularly important during XML validation. When an XML document is validated against a DTD, the XML parser checks whether the number of child elements matches the restrictions defined by the occurrence indicators.

For example:

<!ELEMENT product (name, description?, price)>

The parser expects:

name     → exactly once
description → zero or one time
price    → exactly once

Therefore, this is valid:

<product>
    <name>Laptop</name>
    <price>50000</price>
</product>

And this is also valid:

<product>
    <name>Laptop</name>
    <description>Business laptop</description>
    <price>50000</price>
</product>

But this is invalid:

<product>
    <name>Laptop</name>
    <description>Business laptop</description>
    <description>High-performance computer</description>
    <price>50000</price>
</product>

because description? allows a maximum of one occurrence.

Summary

DTD occurrence indicators provide a simple way to control the number of times an element can appear in an XML document.

The ? indicator makes an element optional while limiting it to a maximum of one occurrence. The * indicator makes an element optional and allows it to occur any number of times. The + indicator requires at least one occurrence but allows additional occurrences.

The key rule to remember is:

?  →  0 or 1
*  →  0 or more
+  →  1 or more

Understanding these three symbols is essential for creating accurate DTD content models because they determine whether elements are mandatory, optional, repeatable, or both optional and repeatable.