HTML and CSS Reference
In-Depth Information
15.4. Element Grammar
The grammar of human language is rich with a variety of sentence struc-
tures, verb tenses, and all sorts of irregular constructs and exceptions
to the rules. Nonetheless, you mastered most of it by the age of three.
Computer language grammars typically are simple and regular, and have
few exceptions. In fact, computer grammars use only four rules to define
how elements of a language may be arranged: sequence, choice, group-
ing, and repetition.
15.4.1. Sequence, Choice, Grouping, and Repetition
Sequence rules define the exact order in which elements appear in a lan-
guage. For instance, if a sequence grammar rule states that element A is
followed by B and then by C, your document must provide elements A,
B, and C in that exact order. A missing element (A and C, but no B, for
example), an extra element (A, B, E, then C), or an element out of place
(C, A, then B) violates the rule and does not match the grammar.
In many grammars, XML included, sequences are defined by simply list-
ing the appropriate elements, in order and separated by commas. Ac-
cordingly, our example sequence in the DTD would appear simply as A,
B, C.
Choice grammar rules provide flexibility by letting the DTD author choose
one element from among a group of valid elements. For example, a
choice rule might state that you may choose elements D, E, or F; any
one of these three elements would satisfy the grammar. Like many other
grammars, XML denotes choice rules by listing the appropriate choices
separated by a pipe character ( | ). Thus, we could write our simple choice
in the DTD as D | E | F. If you read the vertical bar as the word or , choice
rules become easy to understand.
Grouping rules collect two or more rules into a single rule, building richer,
more usable languages. For example, a grouping rule might allow a se-
quence of elements, followed by a choice, followed by a sequence. You
 
Search WWH ::




Custom Search