Domanda

I'm just learning about databases. I'm trying to write a DTD for the following XML data set: http://s3.amazonaws.com/dbclass-resources/docs/countries.xml

At this point, I've tried a few options but am unsuccessful. Can someone please show me how to correctly write a DTD for the above data set (I've pasted a failed attempt of mine below).

<!ELEMENT countries (country*)>
<!ELEMENT country (language?, city?)
    <!ATTLIST country name CDATA #IMPLIED
                      population CDATA #IMPLIED
                      area CDATA #IMPLIED>
    <!ATTLIST language percentage CDATA #IMPLIED
<!ELEMENT city CDATA #IMPLIED>
È stato utile?

Soluzione

There are a few issues that I noticed with your attempt.

The first thing I noticed is that it doesn't look like you know how to use the occurrence indicators correctly. You have country defined as (language?, city?) which means zero or one language followed by zero or one city. This is not what appears in the data. Sometimes there are no city/language elements and sometimes there are more.

I also noticed that you are not closing a few of your declarations with > properly (an example is the declaration for country).

Also, this is invalid:

<!ELEMENT city CDATA #IMPLIED>

It looks like an element declaration and an attribute declaration had a baby. ;-)

Here's an example of a DTD that works that you can use for reference:

<!ELEMENT countries (country)+>

<!ELEMENT country (city*,language*)>
<!ATTLIST country
          area       CDATA #REQUIRED
          name       CDATA #REQUIRED
          population CDATA #REQUIRED>

<!ELEMENT city (name,population)>

<!ELEMENT language (#PCDATA)>
<!ATTLIST language
          percentage CDATA #REQUIRED>

<!ELEMENT name (#PCDATA)>

<!ELEMENT population (#PCDATA)>

Here's a shortcut to a quickref that will help a lot with DTD syntax: http://www.mulberrytech.com/quickref/XMLquickref.pdf

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top