XML Schemas: DTDs

XML Syntax Lecture

Web Services Session 4

Introduction to Document Type Definitions (DTDs)

Well Formed and Valid XML Documents

Internal DTDs

External DTDs

Basic DTD Structure

Basic DTD Structure cont...

DTD Symbols

Example DTD #1

meals.dtd

<!ELEMENT meals ( breakfast, lunch, tea )>
      <!-- meals must contain exactly one of each of the elements
              breakfast, lunch and tea in sequence -->

<!ELEMENT breakfast (food+)>
      <!-- breakfast must contain one or more food items -->

<!ELEMENT lunch (food?)>
      <!-- lunch can be zero or one food item -->

<!ELEMENT tea (food*)>
      <!-- tea can be zero or more food items -->

<!ELEMENT food (fruit | vegetable | fish | poultry | meat | dairy |cereal)>
      <!-- food is one of fruit, veg, fish, poultry, meat, dairy or cereal -->

<!ELEMENT fruit (#PCDATA)>
<!ELEMENT vegetable (#PCDATA)>
<!ELEMENT fish (#PCDATA)>
<!ELEMENT poultry (#PCDATA)>
<!ELEMENT meat (#PCDATA)>
<!ELEMENT dairy (#PCDATA)>
<!ELEMENT cereal (#PCDATA)>

An Example XML Document

DTD For weather.xml

<!-- weather.dtd -->
<!ELEMENT report (weather-station)*>
<!ELEMENT weather-station (location, rainfall, max-temp, min-temp)>
<!ELEMENT location (latitude, longitude, place-name)>
<!ELEMENT latitude (#PCDATA)>
<!ELEMENT longitude (#PCDATA)>
<!ELEMENT place-name (#PCDATA)>
<!ELEMENT rainfall (#PCDATA)>
<!ELEMENT max-temp (#PCDATA)>
<!ELEMENT min-temp (#PCDATA)>
<!-- weather.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE report SYSTEM "weather.dtd">
<report>

<weather-station>
  <location>
     <latitude>-36.82</latitude>
     <longitude>144.30</longitude>
     <place-name>Mandurang</place-name>
  </location>
     
  <rainfall>5.0</rainfall>
  <max-temp>18.1</max-temp>
  <min-temp>6.2</min-temp>
</weather-station>

<weather-station>
  <location>
     <latitude>-36.77</latitude>
     <longitude>144.28</longitude>
     <place-name>Bendigo</place-name>
  </location>
     
  <rainfall>8.0</rainfall>
  <max-temp>19.1</max-temp>
  <min-temp>7.0</min-temp>
</weather-station> 

</report>

Attribute Declarations

<!ATTLIST element attributeName [attributeType] [value] [-- descriptive comment ]>

Syntax for Attribute Declarations

<!ELEMENT x (#PCDATA)>
<!ATTLIST x
    a CDATA #REQUIRED
    b CDATA #IMPLIED
    c CDATA #FIXED "value"
    d CDATA "value" >

<!ELEMENT y (#PCDATA)>
<!ATTLIST y a #REQUIRED>

<!ELEMENT z (someElement)>
<!ATTLIST z a "value"

<!ELEMENT myTag (#PCDATA)>
<!ATTLIST myTag year "2004">
    <!--year is an optional attribute with default value of "2005"-->