One of the more interesting aspects of XML Document Type Definitions (DTDs) is the ability to specify the number of times an element appears within a container. By specifying this information in the DTD, you can improve the usability and validity of your XML documents. Let's look at how to use this feature and examine some of the details of how it works.
Whenever you declare an element in an XML DTD, the XML engine creates an internal symbol for the element. In addition to creating the symbol, the engine attaches rules to the symbol that help the parser determine whether the symbol (or element) is being used correctly, at least as defined by the DTD. One of the rules that the parser attaches to the element’s symbol defines the number of times the element can appear within a certain scope.
By creating rules that inhibit the number of times an element is used, you can better control both the validity and the extensibility of your XML documents. There are four rules that can be attached to each element within a specific scope of the DTD. The four rules are:
- The element can appear only once.
- The element can appear one or more times.
- The element can appear zero or more times.
- The element can appear zero or one time.
To specify that an element can appear only once within a specific context, you need do nothing extra to your DTD. The default rule for all elements in a DTD is that they must appear exactly one time. If your XML document contains no instances of the element, it is invalid. If your XML document contains more than one instance of the element, it is also invalid.
One or more times
To specify that an element can appear one or more times in the document, you use a postfix operator when defining the element’s scope. The postfix operator for one or more elements is the plus sign (+). For example, suppose you have an element that contains an address. You can use the one or more operator to describe to the XML parser that there should be one or more actual <AddressLine> elements. Here’s an example:
<!ELEMENT CustomerAddress (CustomerName, AddressLine+, City, State, Zip)>
Zero or more times
You can also specify that an element can occur zero or more times. As with the one or more operator, you again use a postfix character, in this case, an asterisk symbol (*). For example, suppose your customer document contains a <SecondaryContact> element. Some customers will have no secondary contacts, and others may have multiple secondary contacts. Here’s a simplified example that illustrates the solution:
<!ELEMENT Customer (CustomerData, PrimaryContact, SecondaryContact*) >
Zero or one time
Finally, there’s the zero or one operator. This operator is sometimes called the “optional” operator because it allows an element to be optionally included one time. To specify an element as occurring zero or one time in your document, use the question mark symbol (?). For example, suppose your customer might or might not have an e-mail address. Here’s an example that shows how to handle this situation:
<!ELEMENT Customer(CustomerData, CustomerAddress, PrimaryContact, SecondaryContact*, CustomerEmail?) >
Using DTD rules can help eliminate problems with the data in your XML documents. By specifying the number of times an element can occur, you can better relate your XML data to your business rules. You now know how to use operators in a DTD to specify the rules for handling multiple elements.