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:

Copy to Clipboard
<!ELEMENT element_name (child_element_name*)>

Example:

Copy to Clipboard
<!ELEMENT tutorials (tutorial*)>

One or More

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

Syntax:

Copy to Clipboard
<!ELEMENT element_name (child_element_name+)>

Example:

Copy to Clipboard
<!ELEMENT tutorials (tutorial+)>

Zero or One

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

Syntax:

Copy to Clipboard
<!ELEMENT element_name (child_element_name?)>

Example:

Copy to Clipboard
<!ELEMENT tutorials (tutorial?)>

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:

Copy to Clipboard
<!ELEMENT element_name (choice_1 | choice_2 | choice_3)>

Example:

Copy to Clipboard
<!ELEMENT tutorial (name | title | subject)>

Mixed Content

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

Syntax:

Copy to Clipboard
<!ELEMENT element_name (#PCDATA | child_element_name)>

Example:

Copy to Clipboard
<!ELEMENT tutorial (#PCDATA | name | title | subject)*>

DTD Operators with Sequences

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

Syntax:

Copy to Clipboard
<!ELEMENT element_name (child_element_name dtd_operator, child_element_name dtd_operator,...)>

Example:

Copy to Clipboard
<!ELEMENT tutorial (name+, url?)>

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:

Copy to Clipboard
<!ELEMENT element_name ((sequence) dtd_operator sequence)>

Example:

Copy to Clipboard
<!ELEMENT tutorial ((author,rating?)+ name, url*)>

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