===== Document Type Definition (DTD) ===== This is a cheat sheet for the DTD language. It is a follow up to the [[XML Cheat Sheet]] and summarizes [[http://www.w3schools.com/dtd/default.asp | this DTD Tutorial]]. There is also an [[XML Schema Cheat Sheet]]. For reference this is the [[http://www.w3.org/TR/REC-xml/ | XML Specification]] (which also documents DTD) and the [[http://www.xml.com/axml/testaxml.htm | version annotated by Tim Gray]]. * A **D**ocument **T**ype **D**efinition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes. * A DTD can be internally defined e.g. or externally defined e.g. where the element-declarations are in filename.dtd * A simple example of DTD element-declarations This says that note contains 2 child elements and to and from are of type #PCDATA . * A DTD for an XML document may contain the following building blocks * elements * attributes * entities (e.g. character entities) * PCDATA (parsed character data parsed for entities and markup) * CDATA (character data that is not parsed) ==== DTD Elements ==== * An element is defined as or * category can be either EMPTY or ANY * element-content can be any combination of #PCDATA or child elements * child_element+ - one or more occurrences * child_element* - zero or more occurrences * child_element? - zero or one occurrences * (a|b) - Either a or b (can be #PCDATA) * a,b - a and then b in that order ==== DTD Attributes ==== * An attribute specified in a DTD takes the form for example: * attribute-type can be one of the following * CDATA - character data * (en1|en2|..) - one from an enumerated list * ID - a unique id * IDREF - the id of another element * IDREFS - a list of other ids * NMTOKEN - a valid XML name * NMTOKENS - a list of valid XML names * ENTITY - an entity * ENTITIES - a list of entities * NOTATION - a name of a notation * xml: - a predefined xml value * default-value can be one of the following * value - the default value of the attribute * #REQUIRED - attribute is required but with no default * #IMPLIED - attribute is optional and with no default * #FIXED value - the attribute value is fixed ==== DTD Entities ==== * Entities are variables used to define shortcuts to standard text or special characters. * Entities are declared in DTDs and can be declared internal e.g. or external e.g. * Entity references are references to entities and may be used in XML documents. * An entity reference has three parts: an ampersand (&), an entity name, and a semicolon (;). e.g. &writer;