Evan Evan - 3 months ago 12
SQL Question

Problems with two DTD exercises

I've started a SQL course and I'm in XML right now but can't get past these two exercises that keep giving me errors and I don't know where to find any help! I hope someone can figure out what's wrong! Thanks in advance.

Ex 1 code: https://prod-c2g.s3.amazonaws.com/db/Winter2013/files/courses-ID.xml

My code:

<!ELEMENT Course_Catalog (Department*)>

<!ELEMENT Department (Title, Course+, Professor+, Lecturer?)>
<!ELEMENT Course (Title, Description?)>
<!ELEMENT Description (Courseref)>
<!ELEMENT Professor (First_Name, Middle_Initial?, Last_Name)>
<!ELEMENT Lecturer (First_Name, Middle_Initial?, Last_Name)>

<!ATTLIST Department Code IDREFS #REQUIRED
Chair IDREFS #REQUIRED>
<!ATTLIST Course Number ID #REQUIRED
Prerequisites IDREFS #IMPLIED
Instructors IDREFS #REQUIRED
Enrollment CDATA #IMPLIED>
<!ATTLIST Professor InstrID IDREF #REQUIRED>
<!ATTLIST Lecturer InstrID IDREF #REQUIRED>
<!ATTLIST Courseref Number IDREF #REQUIRED>

<!ELEMENT Title (#PCDATA)>
<!ELEMENT Description (#PCDATA)>
<!ELEMENT First_Name (#PCDATA)>
<!ELEMENT Middle_Initial (#PCDATA)>
<!ELEMENT Last_Name (#PCDATA)>
<!ELEMENT Courseref (#PCDATA)>


Ex 2 code: https://prod-c2g.s3.amazonaws.com/db/Winter2013/files/countries.xml

My code:

<!ELEMENT countries (Country*)>

<!ELEMENT Country (City? | Language?)*>
<!ELEMENT City (Name, Population)>

<!ATTLIST Country Name ID #REQUIRED
Population CDATA #REQUIRED
Area CDATA #REQUIRED>
<!ATTLIST Language Percentage CDATA #REQUIRED>

<!ELEMENT Language (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Population (#PCDATA)>


First one gives me this error:


/tmp/tmpfFfDHT.dtd:19: parser error : ATTLIST: no name for Attribute
<!ELEMENT Title (#PCDATA)>



Second one gives me this error:


/home/sandbox/countries.xml:4: element country: validity error : No
declaration for element country


EDIT: Edited some errors people have pointed out, but both exercises are still giving me errors... :(

Answer

For both examples, I'm assuming only DTD changes are allowed and not changes to the XML.

Example DTD #1

The following DTD changes were necessary for the XML to validate:

  • The element declaration for Department only allows for one occurrence of one or more Professor elements followed by zero or one Lecturer element. I changed this to allow for one or more occurrences of this. (Wrapped Professor+, Lecturer? in parens and added the + occurrence indicator.)
  • There are two element declarations for Description. In the XML, Description has mixed content. Mixed content means it contains both text and child elements. There is only one way to declare mixed content so I updated the first declaration and deleted the second declaration. (See diff below.)
  • There were multiple attributes declared with the attribute type IDREF. If an attribute is declared as an IDREF and is used in the XML, that value also has to exist in an attribute that is declared as an ID. I changed some of the IDREF types to ID for attributes that appeared to be IDs. (See diff below.)

Here is the new DTD:

<!ELEMENT Course_Catalog (Department*)>

<!ELEMENT Department (Title, Course+, (Professor+, Lecturer?)+)>
<!ELEMENT Course (Title, Description?)>
<!ELEMENT Description (#PCDATA|Courseref)*>
<!ELEMENT Professor (First_Name, Middle_Initial?, Last_Name)>
<!ELEMENT Lecturer (First_Name, Middle_Initial?, Last_Name)>

<!ATTLIST Department Code ID #REQUIRED
                     Chair IDREFS #REQUIRED>
<!ATTLIST Course Number ID #REQUIRED
                 Prerequisites IDREFS #IMPLIED
                 Instructors IDREFS #REQUIRED
                 Enrollment CDATA #IMPLIED>
<!ATTLIST Professor InstrID ID #REQUIRED>
<!ATTLIST Lecturer InstrID ID #REQUIRED>
<!ATTLIST Courseref Number IDREF #REQUIRED>

<!ELEMENT Title (#PCDATA)>
<!ELEMENT First_Name (#PCDATA)>
<!ELEMENT Middle_Initial (#PCDATA)>
<!ELEMENT Last_Name (#PCDATA)>
<!ELEMENT Courseref (#PCDATA)>

Here is a diff between your DTD (left) and the modified DTD (right):

exercise 1 diff report

Example DTD #2

The following DTD changes were necessary for the XML to validate:

  • The element names in the DTD all have the first letter capitalized. The XML has all lower-case names. Changed the element names in the DTD to lower-case.
  • In the element declaration for country, the model (city? | language?)* means zero or one city elements or zero or one language elements zero or more times. I simplified this to (city|language)* which means city or language elements zero or more times. This change wasn't required.
  • The name attribute for the country element was declared with the type ID. The actual XML didn't appear to have ID's though. For example Antigua and Barbuda is not a valid ID. I changed this attribute to the CDATA type.

Here is the new DTD:

<!ELEMENT countries (country*)>

<!ELEMENT country (city|language)*>
<!ELEMENT city (name, population)>

<!ATTLIST country name CDATA #REQUIRED
        population CDATA #REQUIRED
        area CDATA #REQUIRED>
<!ATTLIST language percentage CDATA #REQUIRED>

<!ELEMENT language (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT population (#PCDATA)>

Here is a diff between your DTD (left) and the modified DTD (right):

exercise 2 diff report