Either/Or
May 3, 1999
The pipe character is used to specify an "OR" operation. Thus,
the following DTD snippet would specify an XML document in which
all CONTACT elements would have a NAME child followed by either
a PHONE or an EMAIL element (but not both).
<!ELEMENT CONTACT (NAME, (PHONE | EMAIL))>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
Note that XML regular expression matching does not perform
short circuited logic. OR's imply one or the other but not
both and not neither. Is that a tongue twister or what!?!
Using examples to make my point, here are several invalid
XML snippets based on the DTD snippet above....
<CONTACT>
<NAME>Jim Sanger</NAME>
</CONTACT>
That is invalid because the DTD specified that every CONTACT
must have either a PHONE or an EMAIL. The above has neither.
<CONTACT>
<NAME>Jim Sanger</NAME>
<EMAIL>Jim Sanger</EMAIL>
<PHONE>Jim Sanger</PHONE>
</CONTACT>
This one is invalid because the contact has BOTH EMAIL and PHONE
children.
<CONTACT>
<EMAIL>Jim Sanger</EMAIL>
<NAME>Jim Sanger</NAME>
</CONTACT>
This one is wrong because NAME must appear before EMAIL or PHONE.
|
NOTE: within a grouping, you may use only one connector (such as
, or |). Thus, it is invalid to use
<!ELEMENT CONTACT (NAME, PHONE | EMAIL)>
Instead, you must create a subgroup.
<!ELEMENT CONTACT (NAME, (PHONE | EMAIL))>
|
Grouping Elements
Introduction to XML For Web Developers | Table of Contents
Optional Children
|