Defining Web Document Types - Solutions to Exercises

  1. The DTD might be defined as follows:
    <!ELEMENT exam      (code, title, date, questions) >
    <!ELEMENT date      (month, year) >
    <!ELEMENT questions (question, question, question, question, question, question?) >
    <!ELEMENT question  ((part)+) >
    <!ELEMENT part      (#PCDATA|part)* >
    <!ELEMENT code      (#PCDATA) >
    <!ELEMENT title     (#PCDATA) >
    <!ELEMENT month     (#PCDATA) >
    <!ELEMENT year      (#PCDATA) >
    
    A valid instance is:
    <exam>
      <code>IWT</code>
      <title>Internet and Web Technologies</title>
      <date>
        <month>May</month>
        <year>2014</year>
      </date>
      <questions>
        <question><part/></question>
        <question><part/></question>
        <question><part/></question>
        <question><part/></question>
        <question><part/></question>
      </questions>
    </exam>
    
    Instance <exam/> is invalid since the exam element cannot be empty. Instance
    <exam>
      <title>Internet and Web Technologies</title>
      <code>IWT</code>
    </exam>
    
    is invalid since the code element must precede the title element and because the date and questions elements are missing.


  2. One DTD is:
    <!ELEMENT programme   (degree, year, results)>
    <!ELEMENT results     ((distinction)?, (merit)?, (pass)?, (fail)?)>
    <!ELEMENT distinction ((name)*)>
    <!ELEMENT merit       ((name)*)>
    <!ELEMENT pass        ((name)*)>
    <!ELEMENT fail        ((name)*)>
    
    where element names without content models specified are assumed to have #PCDATA as their content model. The results categories need not be optional. Alternatively, (name)+ could be used instead of (name)*, but only if results categories are optional; otherwise there is no way of representing an empty list of names.


  3. With relation attributes represented as elements, the DTD might be:
    <!ELEMENT teaches-relationships (teaches*) >
    <!ELEMENT teaches               (course, lecturer) >
    <!ELEMENT course                (#PCDATA) >
    <!ELEMENT lecturer              (#PCDATA) >
    
    With relation attributes represented as XML attributes, the DTD might be:
    <!ELEMENT teaches-relationships (teaches*) >
    <!ELEMENT teaches               (EMPTY) >
    <!ATTLIST teaches
        course   CDATA #REQUIRED
        lecturer CDATA #REQUIRED >