DTD


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.
  1. !DOCTYPE
  2. !ELEMENT
  3. !ATTLIST
  4. #PCDATA
  5. #REQUIRED
  6. CDATA
  7. NMTOKEN

Symbols in DTD

  • friend One friend
  • friend? Zero or one friend
  • friend+ one friend or more friends
  • friend* zero friends, one friend or more friends

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.

PeopleDtd

PeopleXml

Tip
The people.dtd file sets the following rules in the people.xml file.
  1. Inside the people tag, there must be one or more friend tags, this is indicated by the plus sign in the second line
  2. Each friend must have one first_name, one last_name, one age, one email, one phone and one sex
  3. The first_name is character data

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.

OneFriend

OneFriendXml

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.

emailOptional

EmailOptionalXml

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.

NormalEmailDtd

EmailXml

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.

PeopleEmpty

EmptyDtd

NoErrors

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.

SmokerDtd

SmokerXml

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.

MoreEmailDtd

MoreEmailXml

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.

FriendSsn

SsnXml

Tip
In a DTD file use:
  • <!ELEMENT to set a restriction on an element
  • <!ATTLIST to set a restriction on an attribute

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:
  1. Letters: a, b, c,..., z, A, B, C, ... Z
  2. Numbers: 0, 1, 2, 3, ...
  3. Underscore _
  4. Hypen -
  5. Period .

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.

Nmtoken

NmtokeXml

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.

PeopleID

Duplicates

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home