Databases (HT2014)

Tutorial 6 Solutions

DTDs

First, a schema that directly mirrors the relational model:

<!DOCTYPE Hogwarts [

 <!ELEMENT Hogwarts (Teachers, TeacherTitles, Rooms, Courses, Classes)>
 
 <!ELEMENT Teachers (Teacher*)>
  <!ELEMENT Teacher EMPTY>
   <!ATTLIST Teacher
     name ID    #REQUIRED
     room IDREF #REQUIRED>

 
 <!ELEMENT TeacherTitles (TeacherTitle*) >
  <!ELEMENT TeacherTitle EMPTY >
   <!ATTLIST TeacherTitle
     teacher IDREF #REQUIRED
     title   CDATA #REQUIRED>
 
 <!ELEMENT Rooms (Room*)>
  <!ELEMENT Room EMPTY>
   <!ATTLIST Room
     name    ID    #REQUIRED
     nrSeats CDATA #IMPLIED>

 
 <!ELEMENT Courses (Course*)>
  <!ELEMENT Course EMPTY>
   <!ATTLIST Course
     name       ID    #REQUIRED
     teacher    IDREF #REQUIRED
     nrStudents CDATA #IMPLIED>
 
 <!ELEMENT Classes (Class*)>
  <!ELEMENT Class EMPTY>
   <!ATTLIST Class
     course IDREF #REQUIRED
     day    CDATA #REQUIRED
     hour   CDATA #REQUIRED>

 ]>

Three levels everywhere, for tables, rows, and attributes respectively. Note that we cannot declare composite keys, so Classes and TeacherTitles don't have any ID attributes.

Then a more natural approach:

 <!DOCTYPE Hogwarts [

 <!ELEMENT Hogwarts (Rooms, Teachers, Courses) >
 
 <!ELEMENT Rooms (Room*) >
  <!ELEMENT Room EMPTY >
   <!ATTLIST Room
     name    ID    #REQUIRED
     nrSeats CDATA #IMPLIED >

 
 <!ELEMENT Teachers (Teacher*) >
  <!ELEMENT Teacher (Title*) >
   <!ELEMENT Title (#PCDATA) >
   <!ATTLIST Teacher
     name ID    #REQUIRED
     room IDREF #REQUIRED >
 
 <!ELEMENT Courses (Course*) >
  <!ELEMENT Course (Class*) >

   <!ATTLIST Course
     name       ID    #REQUIRED
     teacher    IDREF #REQUIRED
     nrStudents CDATA #IMPLIED >
   <!ELEMENT Class EMPTY >
    <!ATTLIST Class
      day    CDATA #REQUIRED
      hour   CDATA #REQUIRED >
 
 ]>

There are room for differences of course. We might choose to make Classes children of Rooms instead, and include an IDREF course, to get a model similar in spirit to the one we got when doing the 3NF decomposition in (the solutions of) exercise 3. Similarly we could add an IDREF room to the Class element in the model above, to get a model similar inspirit to the one that we got from the E-R diagram in exercise 2. There are other (endless) variations as well, but these are (in my opinion at least) the most natural.

Files containing both the DTDs and XML data are available on-line:

These XML files have been validated using the W3C validation service.

XML

An XML document conforming to the DTD above:

 <?xml version="1.0" standalone="yes" ?>

 <!-- put the DTD here -->
 
 <Hogwarts>
  <Rooms>
   <Room name="The_Dungeon" nrSeats="34" />
   <Room name="The_Cabin" nrSeats="163" />
  </Rooms>
  <Teachers>
   <Teacher name="Snape" room="The_Dungeon" >
    <Title>Professor</Title>
   </Teacher>
   <Teacher name="Hagrid" room="The_Cabin" />
  </Teachers>
  <Courses>
   <Course name="Potioncraft" teacher="Snape" nrStudents="28">
    <Class day="Monday" hour="10" />
   </Course>
   <Course name="Handling_of_Wild_Creatures" teacher="Hagrid">
    <Class day="Saturday" hour="13" />
    <Class day="Thursday" hour="7" />
   </Course>
  </Courses>
 </Hogwarts>

XPath

(These solutions have been tested using an on-line XPath tester and also the XPath progam on the Linux system at Chalmers.)

XQuery

In some cases I will write more than one answer just to show different ways of writing a query.


Last Modified: 17 September 2014 by Graham Kemp