- Forward


Imposing Structure on XML Documents Using a DTD
An Introduction to Document Type Definitions


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • To be well-formed an XML document only need satisfy a few simple requirements.
  • These requirements say nothing about the structure of the document (e.g., the attributes for each element, the hierarchical structure of the elements).
Document Type Defintions (DTDs)
Back SMYC Forward
  • Purpose:
    • Impose structure on XML documents
  • Associating a DTD with an XML Document:
    • Include a DOCTYPE declaration in the XML document
  • Syntax of the DOCTYPE Declaration:
    • <!DOCTYPE root SYSTEM " dtd " >
    • where root is the root element of the XML document and dtd is the URL of the DTD
DOCTYPE Declaration (in the XML Document)
Back SMYC Forward

An Example

xmlexamples/timetable/dtd/amtrak.xml (Fragment: 0)
 
Declarations in a DTD
Back SMYC Forward
DOCTYPE The root declaration.
ELEMENT Declares an element type.
ATTLIST Declares an attribute list for an element type.
Unary Operators in a DTD
Back SMYC Forward
+ The element can appear one or more times.
* The element can appear zero or more times.
? The element can appear zero or one times.
Binary Operators in a DTD
Back SMYC Forward
| Essentially the delimiter in a list of possible elements.
Types in a DTD
Back SMYC Forward
#PCDATA The element can contain any text (i.e., parsed character data).
EMPTY The element has no content.
ANY The element can have any type of content.
The DOCTYPE Declaration (in a DTD)
Back SMYC Forward
  • Purpose:
    • The root declaration
  • Syntax:
    • <!DOCTYPE root [ declarations ]>
    • where root is the root element of the XML document and declarations are the ELEMENT and ATTLIST declarations that provide the structure
The ELEMENT Declaration (in a DTD)
Back SMYC Forward
  • Purpose:
    • Declares an element type
  • Syntax:
    • <!ELEMENT tag ( contents [, ...]) >
    • where tag is the tag that will be used for this element and content is a description of the allowed contents
The ELEMENT Declaration (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/dtd/timetable.dtd (Fragment: 0)
 
The ATTLIST Declaration (in a DTD)
Back SMYC Forward
  • Purpose:
    • Declares an attribute list for an element type
  • Syntax:
    • <!ATTLIST tag attribute values #modifier [" default "] >
    • where tag is the relevant tag; attribute is the name of the attribute being declared; values is a description of the allowed values (or CDATA for any text), modifier is either REQUIRED, IMPLIED (i.e., optional with no default) or FIXED; and default is the default value of this attribute.
The ATTLIST Declaration (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/dtd/timetable.dtd (Fragment: 1)
 
A Complete Example
Back SMYC Forward

The DTD

xmlexamples/timetable/dtd/timetable.dtd
 

The XML

xmlexamples/timetable/dtd/amtrak.xml
 
An Important Note
Back SMYC Forward
  • Most browsers do not have a validating XML parser
  • If you want to validate an XML document against a DTD use (or download) the JavaScript-based validator or the VBScript-based validator
There's Always More to Learn
Back -