About Modifiers in Element Definitions in DTDs

When you define an element, you specify one or more modifiers. A modifier specifies a rule about the structure or occurrence of the element being defined. An element can have only one top-level modifier. However, you can add one or more modifiers to the top-level modifier. A modifier can aggregate elements or other modifiers.

This section discusses the following topics:

Description of Element Modifiers in DTDs

Table 54 describes the available modifiers:

Modifier
Description
Indicator in DTD
Optional
This element can appear once or not at all. (0 or 1)
Question mark ( ?)
Zero or more
This element is optional and repeatable. (0, 1, or more)
Asterisk ( *)
One or more
This element is required and repeatable. (1 or more)
Plus sign ( +)
Choice
Exactly one of the specified subelements must appear.
Vertical bar ( |)
Sequence
If no other modifiers are defined on the Sequence modifier, each subelement in this element must appear exactly once. In other words, it is required. Also, the subelements must appear in the order in which they are specified in the referencing element. You can define other modifiers on the Sequence modifier. In this way, you can specify that some subelements are optional, some appear zero or more times, and some appear one or more times.
Comma ( ,)
Table 54. Element Modifiers

Simple Example of Aggregating Modifiers in DTDs

Suppose you want a book element to always contain exactly one title element and any number of author elements. The title and author elements contain only raw data. To accomplish this, you would perform steps that generate the following tree representation:

book
               

              
Sequence
                   
title
                   
One or More
                   

                
author title
Zero or More
                   
#PCDATA
                   

                
author
Zero or More
                   
#PCDATA
                   

                

In the book element definition, Sequence modifies book and One or More modifies Sequence. Because the title element immediately follows the Sequence modifier, the default occurrence rule is assumed. That is, the title element must appear exactly once. In the Text view of the DTD, the definition for the book element is as follows:

<!ELEMENT book (title, (author)+)>
       

    

More Complex Example of Aggregating Modifiers in DTDs

Following is a more complicated example. Suppose you want book elements to include

  • Exactly one title
  • Either an author or an editor, but it is okay if neither appear
  • Zero or more paragraphs

To accomplish this, you would perform steps that generate the following tree representation:

book
               

              
Sequence
                   
title
                   
Optional
                   
	Choice
                   
		author
                   
		editor
                   
Zero or More
                   
	paragraph
                   

                

In the Text view of the DTD, the definition for the book element is as follows:

<!ELEMENT book (title, (author|editor)?, paragraph*)>
               

            

Aggregating Modifiers to Allow Any Order and Any Number in DTDs

The Choice modifier specifies that only one of the specified elements can appear in an instance document. However, if you specify the Zero or More modifier and then the Choice modifier, the result is that the specified elements can appear in any order and each element can appear any number of times.

The text for such an element definition is as follows:

<!ELEMENT A (B|C|D)*>
               

            

The tree representation is as follows:

A
               

              
Zero or More
                   
Choice
                   
	B
                   
	C
                   
	D
                   

                

This allows an A element to contain

  • Zero, one, or more B elements
  • Zero, one, or more C elements
  • Zero, one, or more D elements

Furthermore, the contained elements can be in any order.

 
Free Stylus Studio XML Training: