Quackit Logo
HTML
CSS
Scripting
Database
Hosting
Design
XML

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:

OperatorSyntaxDescription
+a+One or more occurences of a
*a*Zero or more occurences of a
?a?Either a or nothing
,a, ba followed by b
|a | ba followed by 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:

Code

Example:

Code

One or More

To allow one or more of the same child element, use a plus sign (+):

Syntax:

Code

Example:

Code

Zero or One

To allow either zero or one of the same child element, use a question mark (?):

Syntax:

Code

Example:

Code

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:

Code

Example:

Code

Mixed Content

You can use the pipe (|) operator to specify that an element can contain both PCDATA and other elements:

Syntax:

Code

Example:

Code

DTD Operators with Sequences

You can apply any of the DTD operators to a sequence:

Syntax:

Code

Example:

Code

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:

Code

Example:

Code

The above example specifies that the "tutorial" element can contain one or more "author" elements, with each occurence having an optional "rating" element.

Enjoy this page?

  1. Link to this page (copy/paste into your own website or blog):
  2. Link to Quackit using one of these banner ads.

Thanks for supporting Quackit!

Sponsored Link: Design CODE-FREE Websites