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:
|+||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 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 (
<!ELEMENT element_name (child_element_name*)>
<!ELEMENT tutorials (tutorial*)>
One or More
To allow one or more of the same child element, use a plus sign (
<!ELEMENT element_name (child_element_name+)>
<!ELEMENT tutorials (tutorial+)>
Zero or One
To allow either zero or one of the same child element, use a question mark (
<!ELEMENT element_name (child_element_name?)>
<!ELEMENT tutorials (tutorial?)>
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:
<!ELEMENT element_name (choice_1 | choice_2 | choice_3)>
<!ELEMENT tutorial (name | title | subject)>
You can use the pipe (
|) operator to specify that an element can contain both PCDATA and other elements:
<!ELEMENT element_name (#PCDATA | child_element_name)>
<!ELEMENT tutorial (#PCDATA | name | title | subject)*>
DTD Operators with Sequences
You can apply any of the DTD operators to a sequence:
<!ELEMENT element_name (child_element_name dtd_operator, child_element_name dtd_operator,...)>
<!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.
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:
<!ELEMENT element_name ((sequence) dtd_operator sequence)>
<!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?
- Link to this page (copy/paste into your own website or blog):
- Link to Quackit using one of these banner ads.
Thanks for supporting Quackit!