DTD |
A Document Type Definition establishes a set of rules so warranty that an XML file meets some specific requirements. As an XML file is used to make transaction between computers, a DTD file ensures that an XML file is in the correct format. The following table shows the main keywords used in a DTD file.
|
Symbols in DTD |
|
Problem 1 |
Create a DTD file called people.dtd to ensure that file has one or more friends in the file. Please be sure that the people.dtd file is UTF-8. |
Tip |
The people.dtd file sets the following rules in the people.xml file.
|
Tip |
To force Microsoft Visual Studio to re-load the people.dtd file, you must temporarily remove the people.dtd file reference at the top of the people.xml file and re-write it again. |
Problem 2 |
Edit the people.dtd file by removing the plus sign after friend. This will imply that there must be only one friend inside the people tag. Microsoft Visual Studio will display the error as shown. Observe that the people.xml file is the same of the previous section without the Social Security Number (ssn) Attribute. |
Problem 3 |
Edit the people.dtd file to make the email field optional. That is, it is possible to provide zero or one email address. Observe that we deleted the email of Mary from the people.xml file. |
Problem 4 |
Edit the people.dtd file and remove the question mark after the email. Do not restore the email address of Mary, you will see an error as shown below. |
Problem 5 |
Delete all friends from the people.xml file. You must get the following error. Then, edit the people.dtd file so that the file can have zero, one or more friends. |
Problem 6 |
Restore the people.xml file to its originals values (Ctrl+Z). Then, edit the people.dtd file so that each friend may provide a sex tag or a smoker tag as shown. Observe that we have removed the sex tag from Jim, and replaced it with a smoker tag. Observe also that it is possible to add comments to a DTD file using the same format as an XML file. |
Problem 7 |
Restore the people.xml file to its original values. Then, edit the people.dtd file so that each friend may provide one or more email addresses. Observe that we have added another email address to Jim Tore. If you replace the + sign after the email with an asterisk, the DTD file will allow zero, one or more email addresses. |
Problem 8 |
Restore the people.xml file to its original values and add the Social Security Number (SSN) as shown. Then, edit the people.dtd file so that each friend may have a required SSN attribute. If you remove the SSN from any of the friends you will get an error. |
Tip |
In a DTD file use:
|
NMTOKEN |
An NMTOKEN is very similar to a way you declare a variable in a programming language. An NMTOKEN declares a token which the valid characters are:
|
Problem 9 |
Restore the people.xml file to its original values and add the Social Security Number (SSN) as shown. Then, edit the people.dtd file so that each friend may have a required SSN attribute; and the SSN attribute must be a token. You may try to add a space to the SSN, you will get an error as shown below. |
Tip |
An attribute may be restricted to a set of possible values as shown below <!ATTLIST sex (MALE | FEMALE) #REQUIRED > |
Problem 10 |
An attribute may be used as a key (similar to a primary key in a Relational Database). Thus, key duplicates values are not allowed. Modify the people.dtd file to set the SSN attribute as unique key. You may try to insert a duplicate SSN, you will get an error as shown. Observe that an ID must begin with a letter or an underscore; you may add an underscore at the beginning of the SSN to correct the error. |