JMU
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


Motivation
Document Type Defintions (DTDs)
DOCTYPE Declaration (in the XML Document)

An Example

xmlexamples/timetable/dtd/amtrak.xml (Fragment: 0)
        <!DOCTYPE timetable SYSTEM "timetable.dtd">
        
Declarations in a DTD
DOCTYPE The root declaration.
ELEMENT Declares an element type.
ATTLIST Declares an attribute list for an element type.
Unary Operators in a DTD
+ 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
| Essentially the delimiter in a list of possible elements.
Types in a DTD
#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)
The ELEMENT Declaration (in a DTD)
The ELEMENT Declaration (cont.)

An Example

xmlexamples/timetable/dtd/timetable.dtd (Fragment: 0)
        <!ELEMENT stop (station, time*) >
        
The ATTLIST Declaration (in a DTD)
The ATTLIST Declaration (cont.)

An Example

xmlexamples/timetable/dtd/timetable.dtd (Fragment: 1)
        <!ELEMENT train (stop+) >
  <!ATTLIST train businessclass (YES | NO) "NO" >
  <!ATTLIST train normaldays (Daily | Mo-Fr | Sa | Su) #REQUIRED >
  <!ATTLIST train number CDATA #REQUIRED >
  <!ATTLIST train reservations (YES | NO) "NO" >
        
A Complete Example

The DTD

xmlexamples/timetable/dtd/timetable.dtd
        <!ELEMENT timetable (train*) >
  <!ATTLIST timetable subtitle CDATA #IMPLIED>
  <!ATTLIST timetable title CDATA #IMPLIED>



<!ELEMENT station (#PCDATA) >

<!ELEMENT stop (station, time*) >



<!ELEMENT time (#PCDATA) >
  <!ATTLIST time status (Ar | Dp) #IMPLIED>


<!ELEMENT train (stop+) >
  <!ATTLIST train businessclass (YES | NO) "NO" >
  <!ATTLIST train normaldays (Daily | Mo-Fr | Sa | Su) #REQUIRED >
  <!ATTLIST train number CDATA #REQUIRED >
  <!ATTLIST train reservations (YES | NO) "NO" >

        

The XML

xmlexamples/timetable/dtd/amtrak.xml
        <?xml version="1.0"?>
<!DOCTYPE timetable SYSTEM "timetable.dtd">

<timetable title="Boston...Providence...New London...New York"
     subtitle="Through services to Philadelphia...Washington...Newport News">
  <train number="95" normaldays="Mo-Fr" reservations="YES" businessclass="YES">
    <stop>
      <station>Boston, MA-South Station</station>
      <time>6:20AM</time>
    </stop>

    <stop>
      <station>Boston, MA-Back Bay</station>
      <time>6:25AM</time>
    </stop>

    <stop>
      <station>Route 128, MA</station>
      <time>7:07AM</time>
    </stop>

    <stop>
      <station>Providence, RI</station>
      <time>7:31AM</time>
    </stop>

    <stop>
      <station>Kingston, RI</station>
      <time></time>
    </stop>

    <stop>
      <station>Westerly, RI</station>
      <time></time>
    </stop>

    <stop>
      <station>Mystic, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>New London, CT</station>
      <time>8:06AM</time>
    </stop>

    <stop>
      <station>Old Saybrook, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>New Haven, CT</station>
      <time status="Ar">9:00AM</time>
      <time status="Dp">9:15AM</time>
    </stop>

    <stop>
      <station>Bridgeport, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>Stamford, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>New Rochelle, NY</station>
      <time></time>
    </stop>

    <stop>
      <station>New York, NY</station>
      <time status="Ar">10:50AM</time>
    </stop>

    <stop>
      <station>Newark, NJ</station>
      <time>11:26AM</time>
    </stop>

    <stop>
      <station>Metropark, NJ</station>
      <time>11:40AM</time>
    </stop>

    <stop>
      <station>Trenton, NJ</station>
      <time>12:04AM</time>
    </stop>

    <stop>
      <station>Philadelphia, PA</station>
      <time>12:32PM</time>
    </stop>

    <stop>
      <station>Wilmington, DE</station>
      <time>12:58PM</time>
    </stop>

    <stop>
      <station>Baltimore, MD</station>
      <time>1:49PM</time>
    </stop>

    <stop>
      <station>BWI Airport Rail Sta., MD</station>
      <time>2:02PM</time>
    </stop>

    <stop>
      <station>New Carrollton, MD</station>
      <time>2:17PM</time>
    </stop>

    <stop>
      <station>Washington, DC</station>
      <time>2:35PM</time>
    </stop>

    <stop>
      <station>Richmond, VA</station>
      <time>5:19PM</time>
    </stop>

    <stop>
      <station>Newport News, VA</station>
      <time status="Ar">7:07PM</time>
    </stop>
  </train>




  <train number="191" normaldays="Sa"  reservations="YES" businessclass="YES">
    <stop>
      <station>Boston, MA-South Station</station>
      <time>6:20AM</time>
    </stop>

    <stop>
      <station>Boston, MA-Back Bay</station>
      <time>6:25AM</time>
    </stop>

    <stop>
      <station>Route 128, MA</station>
      <time>7:07AM</time>
    </stop>

    <stop>
      <station>Providence, RI</station>
      <time>7:31AM</time>
    </stop>

    <stop>
      <station>Kingston, RI</station>
      <time></time>
    </stop>

    <stop>
      <station>Westerly, RI</station>
      <time></time>
    </stop>

    <stop>
      <station>Mystic, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>New London, CT</station>
      <time>8:06AM</time>
    </stop>

    <stop>
      <station>Old Saybrook, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>New Haven, CT</station>
      <time status="Ar">9:00AM</time>
      <time status="Dp">9:15AM</time>
    </stop>

    <stop>
      <station>Bridgeport, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>Stamford, CT</station>
      <time></time>
    </stop>

    <stop>
      <station>New Rochelle, NY</station>
      <time></time>
    </stop>

    <stop>
      <station>New York, NY</station>
      <time status="Ar">10:50AM</time>
    </stop>

    <stop>
      <station>Newark, NJ</station>
      <time>11:26AM</time>
    </stop>

    <stop>
      <station>Metropark, NJ</station>
      <time>11:40AM</time>
    </stop>

    <stop>
      <station>Trenton, NJ</station>
      <time>12:04AM</time>
    </stop>

    <stop>
      <station>Philadelphia, PA</station>
      <time>12:32PM</time>
    </stop>

    <stop>
      <station>Wilmington, DE</station>
      <time>12:58PM</time>
    </stop>

    <stop>
      <station>Baltimore, MD</station>
      <time>1:49PM</time>
    </stop>

    <stop>
      <station>BWI Airport Rail Sta., MD</station>
      <time>2:02PM</time>
    </stop>

    <stop>
      <station>New Carrollton, MD</station>
      <time>2:17PM</time>
    </stop>

    <stop>
      <station>Washington, DC</station>
      <time>2:35PM</time>
    </stop>

    <stop>
      <station>Richmond, VA</station>
      <time></time>
    </stop>

    <stop>
      <station>Newport News, VA</station>
      <time></time>
    </stop>
  </train>




  <train number="171" normaldays="Daily" businessclass="YES">
    <stop>
      <station>Boston, MA-South Station</station>
      <time>7:22AM</time>
    </stop>

    <stop>
      <station>Boston, MA-Back Bay</station>
      <time>7:27AM</time>
    </stop>

    <stop>
      <station>Route 128, MA</station>
      <time>7:42AM</time>
    </stop>

    <stop>
      <station>Providence, RI</station>
      <time>8:17AM</time>
    </stop>

    <stop>
      <station>Kingston, RI</station>
      <time>8:43AM</time>
    </stop>

    <stop>
      <station>Westerly, RI</station>
      <time>8:59AM</time>
    </stop>

    <stop>
      <station>Mystic, CT</station>
      <time>9:10AM</time>
    </stop>

    <stop>
      <station>New London, CT</station>
      <time>9:25AM</time>
    </stop>

    <stop>
      <station>Old Saybrook, CT</station>
      <time>9:47AM</time>
    </stop>

    <stop>
      <station>New Haven, CT</station>
      <time status="Ar">10:25AM</time>
      <time status="Dp">10:35AM</time>
    </stop>

    <stop>
      <station>Bridgeport, CT</station>
      <time>10:57AM</time>
    </stop>

    <stop>
      <station>Stamford, CT</station>
      <time>11:23AM</time>
    </stop>

    <stop>
      <station>New Rochelle, NY</station>
      <time>11:43AM</time>
    </stop>

    <stop>
      <station>New York, NY</station>
      <time status="Ar">12:20PM</time>
    </stop>

    <stop>
      <station>Newark, NJ</station>
      <time>12:56PM</time>
    </stop>

    <stop>
      <station>Metropark, NJ</station>
      <time></time>
    </stop>

    <stop>
      <station>Trenton, NJ</station>
      <time>1:34PM</time>
    </stop>

    <stop>
      <station>Philadelphia, PA</station>
      <time>2:02PM</time>
    </stop>

    <stop>
      <station>Wilmington, DE</station>
      <time>2:43PM</time>
    </stop>

    <stop>
      <station>Baltimore, MD</station>
      <time>3:42PM</time>
    </stop>

    <stop>
      <station>BWI Airport Rail Sta., MD</station>
      <time>3:55PM</time>
    </stop>

    <stop>
      <station>New Carrollton, MD</station>
      <time>4:10PM</time>
    </stop>

    <stop>
      <station>Washington, DC</station>
      <time>4:30PM</time>
    </stop>

    <stop>
      <station>Richmond, VA</station>
      <time></time>
    </stop>

    <stop>
      <station>Newport News, VA</station>
      <time></time>
    </stop>
  </train>



</timetable>
        
An Important Note