DTD Element Operators
This article provides an overview of DTD element operators.
One of the examples in the previous lesson demonstrated how to specify that an element (tutorials
) must contain one instance of another element (tutorial
).
This is fine if there only needs one instance of tutorial
, but what if we didn't want a limit. What if the tutorials
element should be able to contain any number of tutorial
instances? Fortunately we can do that using DTD operators.
Here's a list of operators/syntax rules we can use when defining child elements:
Operator | Syntax | Description |
---|---|---|
+ | a+ | One or more occurences of a |
* | a* | Zero or more occurences of a |
? | a? | Either a or nothing |
, | a, b | a followed by b |
| | a | b | a or b |
() | (expression) | An expression surrounded by parentheses is treated as a unit and could have any one of the following suffixes ?, *, or +. |
Examples of usage follow.
Zero or More
To allow zero or more of the same child element, use an asterisk (*
):
Syntax:
Example:
One or More
To allow one or more of the same child element, use a plus sign (+
):
Syntax:
Example:
Zero or One
To allow either zero or one of the same child element, use a question mark (?
):
Syntax:
Example:
Choices
You can define a choice between one or another element by using the pipe (|
) operator. For example, if the tutorial
element requires a child called either name
, title
, or subject
(but only one of these), you can do the following:
Syntax:
Example:
Mixed Content
You can use the pipe (|
) operator to specify that an element can contain both PCDATA and other elements:
Syntax:
Example:
DTD Operators with Sequences
You can apply any of the DTD operators to a sequence:
Syntax:
Example:
The above example allows the tutorial
element to contain one or more instance of the name
element, and zero or one instance of the url
element.
Subsequences
You can use parentheses to create a subsequence (i.e. a sequence within a sequence). This enables you to apply DTD operators to a subsequence:
Syntax:
Example:
The above example specifies that the tutorial
element can contain one or more author
elements, with each occurence having an optional rating
element.