Next page Previous page Start of chapter End of chapter

Document Type Definition

The markup permitted in a particular XML application can be documented in a schema. The most broadly supported schema language and the only one defined by the XML 1.0 specification is the Document Type Definition (DTD). Another common schema language is XSchema.

A DTD allows you to place some constraints on the structure an XML document takes. It lists all the elements, attributes, and entities the document uses and the context in which it uses them. DTDs never say anything about the type of content of an element or of the value of an attribute. For instance, you cannot say that price is a real number, or name is a string, or born is a date.

Recall the XML document about Alan Turing. Here is a DTD for this document:

<!ELEMENT person     (name, profession*)>
<!ATTLIST person      born CDATA #REQUIRED died CDATA #IMPLIED>
<!ELEMENT name       (first,last)>
<!ELEMENT first      (#PCDATA)>
<!ELEMENT last       (#PCDATA)>
<!ELEMENT profession (#PCDATA)>

The first line declares that the element person must contain exactly one child element name and zero or more child elements profession, in this order. The second line says that the same element must have a born attribute and may have a died attribute (the order is not relevant). The third line declares that the element name must have two children called first and last in this order. The last three lines specify that the elements first, last, and profession must contain parsed character data (PCDATA), that is, raw text possibly containing entity and character references but not containing any tags.

Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet