DTD Example Explanation

The example DTD on the previous page is listed again below with explanations where appropriate. The line numbers are not part of the DTD but are provided for reference. Lines 1 through 15 are used to define entities which are used for element declarations. The establishment of these entities makes the text much shorter and easier to use since entities can be used to establish and group both standard elements and common attributes. On lines 17 and 18, below, the entity is used to declare a complete group of elements at one time thus saving a great deal of space and effort. Entities may be contained in external files, but the use of external entities is not shown here.

  1. <!ENTITY % HTML.Version "-//W3C//DTD HTML 4.01 Transitional//EN">
    This specifies the entity "HTML.Version" used later to establish the version of this HTML DTD when the HTML root element is declared.

  2. <!ENTITY % Script "CDATA" -- script expression -->
    This specifies the entity "Script" to be made of character data (CDATA).

  3. <!ENTITY % URI "CDATA" -- a Uniform Resource Identifier, see [URI] -->
    This defines an entity named URI to be made of character data (CDATA).

  4. <!ENTITY % StyleSheet "CDATA" -- style sheet data -->

  5. <!ENTITY % heading "H1|H2|H3|H4|H5|H6">
    This defines an entity "heading" to be made of the character strings "H1|H2|H3|H4|H5|H6". This will be used later to create the header elements H1 through H6 and also it will be used as a group reference for these elements. However, remember for now, that the "heading" is just a string as described and the heading elements are not yet defined.

  6. <!ENTITY % list "UL | OL">
    This defines an entity string for lists, much as the entity string for headers, above.

  7. <!ENTITY % coreattrs
    "id ID #IMPLIED -- document-wide unique id --
    class CDATA #IMPLIED -- space-separated list of classes --
    style %StyleSheet; #IMPLIED -- associated style info --
    title %Text; #IMPLIED -- advisory title --"

    This defines an entity to be used later to establish core attributes for many elements. Notice that the appropriate text for the attribute name, attribute type and attribute default value (in this case #IMPLIED indicating the attributes are optional) are included.

  8. <!ENTITY % events
    "onclick %Script; #IMPLIED -- a pointer button was clicked --
    ondblclick %Script; #IMPLIED -- a pointer button was double clicked--
    onmousedown %Script; #IMPLIED -- a pointer button was pressed down --
    onmouseup %Script; #IMPLIED -- a pointer button was released --
    onmouseover %Script; #IMPLIED -- a pointer was moved onto --
    onmousemove %Script; #IMPLIED -- a pointer was moved within --
    onmouseout %Script; #IMPLIED -- a pointer was moved away --
    onkeypress %Script; #IMPLIED -- a key was pressed and released --
    onkeydown %Script; #IMPLIED -- a key was pressed down --
    onkeyup %Script; #IMPLIED -- a key was released --"

    This defines an event entity named which is used later to establish event attributes to elements where it is apporpriate.

  9. <!ENTITY % attrs "%coreattrs; %events;">
    This defines an entity named "attrs" which is made of the combination of the coreattrs and events entities. It is used later to establish these entities to various elements as they are declared.

  10. <!ENTITY % align "align (left|center|right|justify) #IMPLIED">
    This is an entity used to establish alignment attributes for some elements.

  11. <ENTITY % phrase
    | KBD | VAR | CITE | ABBR | ACRONYM" >

    This entity is used to reference and create elements classified as phrase elements.

  12. <!ENTITY % fontstyle
    "TT | I | B | U | S | STRIKE | BIG | SMALL">

    This entity is used to reference and create elements classified as elements used to set font style.

  13. <!ENTITY % inline
    "#PCDATA | %phrase; | %fontstyle;">

    This entity is used to define inline elements to be a combination of phrase elements, font style elements, and PCDATA (Parsed character data).

  14. <ENTITY % block
    "P | %heading; | %list; | HR">

    This entity is used to define elements to be used as block elements. It includes the paragraph (P) element, HR, and heading and list elements defined in entities, above. Entities must be defined before they can be used.

  15. <!ENTITY % flow "%block; | %inline;">
    This entity is used to define flow elements which include all block and inline elements. In this example, flow elements include all elements except the BODY element.

  16. <ELEMENT BODY O O (%flow;)* -- document body -->
    <ATTLIST BODY %attrs; -- %coreattrs, %i18n, %events --
    onload %Script; #IMPLIED -- the document has been loaded --
    onunload %Script; #IMPLIED -- the document has been removed --
    background %URI; #IMPLIED -- texture tile for document background --

    This is where the BODY element is declared along with its attributes. The body element requires neither a starting nor an ending tag. It is allowed to contain any combination of flow elements (the * character indicates this). The entities "Script", and "URI" are used to help define the content of the onload, onunload, and background attributes.

  17. <!ELEMENT (%fontstyle; | %phrase;) - - (%inline;)*>
    <!ATTLIST (%fontstyle; | %phrase;)
    %attrs; -- %coreattrs, %events --

    This is where the fontstyle and phrase elements are declared using the "fontstyle" and "phrase" entities. Using these entities saves much space and trouble by allowing the complete set of entities to be declared on one line. Also the "- -" indicates a start tag and end tag are required. The fontstyle and phrase elements are allowed to contain any inline elements which in the case of this example include the fontstyle and phrase elements. In the case of this example, I could have done the same declaration by substituting the string "(%inline;)" for the string "(%fontstyle; | %phrase;)", above. Also note the string "%attrs" is used to create the common attributes for this element.

  18. <!ELEMENT (%heading;) - - (%inline;)* -- heading -->
    >!ATTLIST (%heading;)
    %attrs; -- %coreattrs, %i18n, %events --
    %align; -- align, text alignment -- >

    The header elements (H2 through H6) are declared here using the "heading" entity. The "attrs" and "align" entities are used to setup common and alignment attributes.

  19. <!ENTITY % Length "CDATA" -- nn for pixels or nn% for percentage length -->
  20. <!ENTITY % Pixels "CDATA" -- integer representing length in pixels -->
    The two entities listed above establish the Length and Pixel entities to contain character data..

  21. <!ELEMENT HR - O EMPTY -- horizontal rule -->
    %attrs; -- %coreattrs, %i18n, %events --
    align (left|center|right) #IMPLIED
    noshade (noshade) #IMPLIED
    size %Pixels; #IMPLIED
    width %Length; #IMPLIED

    The HR element is an empty element and the word "EMPTY" in the element declaration is used to indicate this. Several attributes are defined using entities and some are defined using text. The #IMPLIED word indicates that the attribute is not required.

  22. <!ELEMENT P - O (%inline;)* -- paragraph -->
    %attrs; -- %coreattrs, %i18n, %events --
    %align; -- align, text alignment --

    The paragraph element requires a start tag but does not require an end tag.

  23. <!ENTITY % OLStyle "CDATA" -- constrained to: "(1|a|A|i|I)" -->
  24. <!ELEMENT OL - - (LI)+ -- ordered list -->
    %attrs; -- %coreattrs, %i18n, %events --
    type %OLStyle; #IMPLIED -- numbering style --
    compact (compact) #IMPLIED -- reduced interitem spacing --
    start NUMBER #IMPLIED -- starting sequence number --

    The ordered list (OL) entity requires a start and end tag. It must contain one or more LI elements.

  25. <!ENTITY % ULStyle "(disc|square|circle)">
    <!ELEMENT UL - - (LI)+ -- unordered list -->
    %attrs; -- %coreattrs, %i18n, %events --
    type %ULStyle; #IMPLIED -- bullet style -- compact
    (compact) #IMPLIED -- reduced interitem spacing -- >

  26. <!ENTITY % LIStyle "CDATA" -- constrained to: "(%ULStyle;|%OLStyle;)" -->
  27. <!ELEMENT LI - O (%flow;)* -- list item -->
    %attrs; -- %coreattrs, %i18n, %events --
    type %LIStyle; #IMPLIED -- list item style --
    value NUMBER #IMPLIED -- reset sequence number --

    The LI element requires a start tag, but not an ending tag. It may contain any combination of flow elements which are all the elements defined in this DTD except for the BODY element.

  28. <!ENTITY % version "version CDATA #FIXED '%HTML.Version;'">
  29. <!ENTITY % html.content "HEAD, BODY">
  30. <!ELEMENT HTML O O (%html.content;) -- document root element -->
    %version; >

    This is the root element which is called HTML and is the same as the name following the DOCTYPE keyword in the HTML document. This element is required and is the single containing element in an HTML file. It requires meither a beginning nor ending tag since the tags are assumed. The required tags are HEAD and BODY and they must be in order with the HEAD first. The BODY declaration and associated tags were left out of this document to keep it brief.

DTD Contents Page