Declaring Elements in DTDs

Defining the structure of an XML document must always involve the declaration of elements because all XML documents contain at least one element: the document element.

An element declaration takes this general form:

<!ELEMENT elementTypeName contentModel > 

The allowed content models are listed here:

  • Text only— Indicated by (#PCDATA). No element content is allowed.

  • Empty element— Indicated by EMPTY. Not even whitespace is allowed as content.

  • Any content— Indicated by ANY. Any well-formed content is allowed.

  • Mixed content— Indicated by MIXED. This allows text content to be mixed with element content declared in the element declaration.

  • Child elements— Indicated by one or more element names contained in parentheses, with any appropriate cardinality indicators.

Consider a simple XML document, such as the following:

<simpleMessage> 
Here is a simple message. 
</simpleMessage> 

The following element declaration could be used to indicate that the simpleMessage element may contain only parsed character data, indicated by #PCDATA in the element declaration.

<!ELEMENT simpleMessage (#PCDATA) > 

The simpleMessage element is constrained to contain parsed character data only. The presence of any elements in the content of the simpleMessage element would render that instance document invalid.

Typically, you would want to allow element content within a document element. To do so, you must declare the allowed elements. Consider an instance document with the following structure:

<book> 
<title>Sams Teach Yourself XML in 10 Minutes</title> 
<author>Andrew Watt</author> 
<publisher>Sams Publishing</publisher> 
</book> 

You could indicate that a book element is allowed to contain a title element, an author element, and a publisher element, in that order, as follows:

<!ELEMENT book (title, author, publisher) > 

You have not yet defined the allowed content of the title, author, and publisher elements—in this case, each has parsed character data. You would do so by completing the DTD as follows:

<!ELEMENT book (title, author, publisher) > 
<!ELEMENT title (#PCDATA) > 
<!ELEMENT author (#PCDATA) > 
<!ELEMENT publisher (#PCDATA) > 

The preceding DTD indicates that the book element is the document element. It may contain one and exactly one title element, followed by a single author element, then followed by a single publisher element.

In many settings, an element has more than one child element of a particular element type. Thus, you need ways to express the allowed frequency of occurrence—the cardinality—of child elements.

Cardinality

In XML 1.0, the default cardinality is exactly one occurrence of, for example, an element. Therefore, the absence of any of the cardinality operators in the following list indicates that an element is allowed to occur exactly once.

In addition, the DTD can express three choices of cardinality that must be explicitly expressed within markup declarations:

  • Optional, but may only occur once at most— Zero or one occurrences. This is indicated by the ? character.

  • Optional, but may occur many times— A minimum of zero occurrences and an unlimited maximum. This is indicated by the * character.

  • Required, but may occur many times— A minimum of one occurrence and an unlimited maximum. This is indicated by the + character.

Note

A DTD cannot express the notion that an element must occur, say, a minimum of 3 times and a maximum of 20 times.



For example, consider a customer order with a structure similar to the following:

<order> 
<date> 
2003/04/01 
</date> 
<customerID> 
AB987 
</customerID> 
<items> 
<item productID="1234" quantity="10"> 
3.5" floppy disks 
</item> 
<item productID="2345" quantity="20"> 
Write once CDROMs 
</item> 
</items> 
<customerComment>I need the floppy disks as soon as possible. 
</customerComment> 
<customerComment> 
Don't attempt delivery on a Friday. 
</customerComment> 
</order> 

You could express that using the following markup declarations:

<!ELEMENT order (date, customerID, items, customerComment*) > 
<!ELEMENT date (#PCDATA)> 
<!ELEMENT customerID (#PCDATA) > 
<!ELEMENT items (item)+ > 
<!ELEMENT customerComment (#PCDATA) > 
<!ELEMENT item (#PCDATA) > 
<!ATTLIST item 
 productID CDATA #REQUIRED 
 quantity CDATA #REQUIRED> 

In the first line of the code, the date, customerID, and items elements are declared without any cardinality operator. They are required and can occur only once. The customerComment element is declared with a * cardinality operator, indicating that it is optional but can occur more than once.

The declaration of the items element specifies that there must be at least one item element as its child, but that the item element may occur more than once.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset