< Previous | Next >

Lesson 1.2: Customize your DTD

This exercise teaches you how to create and modify elements and attributes in a DTD file.
Before you begin, you must complete Lesson 1.1: Create your DTD.
In this lesson, you will define the following elements in your DTD:
Element Description
CDLib The CD library root element that contains all the other elements.
CD A container element that contains information about a CD.
artist An element that contains the name of a musician on the CD.
title An attribute that describes the title of the CD.
format An element that describes the number of CDs for this title.
description An element that provides a description of the CD.
price An element that provides the price of the CD.

Create and name elements

Follow these steps to add the elements listed in the table above to your DTD:
  1. In the Outline view, right-click the DTD and click Add Element.
  2. In the Properties view, change the name of the element to CDLib.
  3. Right-click the DTD again, and click Add Element. In the Properties view, change the name of the element to CD.
  4. Repeat step 3 to create the following elements: artist, format, description, and price.

Edit an element's content model

An element's content model is the representation of any data that might be contained inside an element. When you create an element, a node automatically appears in the Outline view to represent the content model type of the element, which by default is EMPTY (does not contain any other elements or text). You need to change the value of each element:
  1. In the Outline view, under CDLib, select EMPTY. In the Properties view, click the Other tab. In the content type lists, select CD.
  2. In the Outline view, under the parent element CD (not the child element under CDLib), select EMPTY.
  3. In the Properties view, select Children Content from the Content type list. This adds a group node under the CD element.
  4. Expand the group node and select Add Element to Content Model. Then, in the Properties view General tab, select artist from the Content Model list.
  5. Right-click the group node. From its pop-up menu, select Add Element to Content Model. This adds another child element.
  6. Select the new child element. In the Properties view for it, select format from the Content Model list.
  7. Repeat steps 5 and 6 to create description and price child elements.
  8. Click the parent element named artist (not the child element under CD) and expand it.
  9. Click EMPTY. In the Properties view, click the Other tab. From the content type list, select (#PCDATA).
  10. Repeat steps 8 and 9 for the format, description, and price elements.

You have now specified the content models for each of the elements. A CDLib element can only contain CD elements, whereas a CD element can contain the artist, format, description, and price elements. The artist, format, description, and price elements cannot contain any other elements, they can only contain text.

Since you wanted the CD element to be able to contain more than one element, you defined its Content type to be Children Content. This caused a group node to be automatically added to the CD element. The group node enables you to add as many elements to an element's content model as you like. It also determines whether the elements are sorted in sequence or are available as a choice (you can only select one option or the other) and how often the group of elements can occur.

Edit an element's attributes

A CD library typically contains more than one CD. To indicate this in your DTD, you need to change the content model for the CDLib element:

  1. In the Outline view, select the CD child element that is under the CDLib element. In the Properties view, click the Attributes tab. The Properties view shows the occurrence of its content model to be Just once (that is, CDLib can only contain one CD element).
  2. Click One or more from the Occurrence drop down list to change its content model to contain one or more CDs.
  3. Review the contents of the Source view. The CDLib element should look like this: <!ELEMENT CDLib (CD+)>

The CD library can now contain as many CDs as you want to add to it.

Edit a child element's content model

You also need to make some changes to the artist and the format content models:

  1. For a CD, there can be more than one contributing artist. Indicate this by selecting the artist child element (that is, the artist element that is listed under CD) and changing its occurrence to be One or more.
  2. Most titles fit on one CD. For such titles, the format element is optional. Indicate this by selecting the format child element and change its occurrence to be Optional.
The resulting CD element should look like this (in the Source view):
<!ELEMENT CD (artist+,format?,description,price)>

Add attributes

You now need to add the attribute title to the CD element (the parent element, not the child element under CDLib). To do so, follow these steps:
  1. In the Outline view, right-click the CD parent element, and select Add Attribute. Note: All elements must have their attributes listed in an attribute list, therefore, if you add an attribute to an element that does not already have an attribute list, an attribute list will automatically be created and the new attribute added to it.
  2. Expand the attribute list and select NewAttribute. In the Properties view, change the name of the attribute to title. The attribute type value defaults to Character Data (CDATA), and the attribute default value should already be set to #IMPLIED.
  3. To save the DTD, select File > Save.
You have now completed your CDLib definition. It should look similar to the following:

Lesson checkpoint

The DTD editor allows you to efficiently create, and edit, elements and attributes.
Now that you have completed this lesson, you should be able to:
  • Create and name elements.
  • Edit element's content models.
  • Add attributes to elements.
< Previous | Next >

Feedback