Defining a DTD - Getting Started

This section provides a quick tour of the main features of the DTD Editor. It provides instructions that you can follow to actually define a simple DTD. For complete documentation about how to use the Stylus Studio DTD Editor, see Chapter 8Defining Document Type Definitions.

Process Overview

When you use Stylus Studio to define a DTD, the main steps you perform are:

1. Create a new DTD schema file.
2. Define the elements that contain the raw data.
3. Define the elements that contain other elements.
4. In the container elements, specify the rules for the contained elements. That is, specify whether a contained element is optional or required, whether there can be more than one, and what order contained elements must be in.

This section provides step-by-step instructions for defining the bookstore.dtd schema file. You should perform the steps in each topic in the order of the topics. This section includes the following topics:

Before you begin

To get started, you'll need to start Stylus Studio if you haven't already. See Starting Stylus Studio.

Creating a Sample DTD

To create a new DTD schema file:
1. From the Stylus Studio menu bar, select File > New > DTD Schema.
2. Click Save .

Stylus Studio displays the Save As dialog box.

3. Navigate to the Stylus Studio examples directory.
4. In the URL: field, type bookstore.dtd.
5. Click the Save button.

Defining Data Elements in a Sample DTD

This topic is part of a sequence that starts with Creating a Sample DTD.

In your DTD, suppose you want a book element to be optional. Further, if a book element is present, it must always have exactly one title element and it can have any number of author elements. The title and author elements contain only raw data.

To accomplish this, perform the following steps:
1. At the bottom of the DTD editor, click the Tree tab.
2. Click the DTD node at the top of the tree if it is not already selected.
3. Click New Element Definition , which is the top button in the tool bar on the left side of the DTD editor window.

Stylus Studio displays an entry field for the element name.

4. Type title and press Enter.

Stylus Studio displays the new element, title, and the element's properties in the Properties window.

Figure 64. New Element in the DTD Editor

5. Click New Modifier .

Stylus Studio displays an entry field for the element's modifier.

6. Double-click Zero or More.

The new modifier is added to the element.

7. Click Add #PCDATA .
8. To define the author element, repeat step 2 through step 7. In step 4, type author instead of title.

When you are done, the Stylus Studio desktop should resemble the following:

i

Figure 65. Creating a DTD with Two Elements

Defining the Container Element in a Sample DTD

This topic is part of a sequence that starts with Creating a Sample DTD.

To define the book element:
1. Click the DTD node at the top of the tree.
2. Click New Element Definition .
3. Type book and press Enter.

Defining Structure Rules in a Sample DTD

This topic is part of a sequence that starts with Creating a Sample DTD.

To specify the rules for the structure of the book element:
1. Click the book node in the DTD tree if it is not already selected.
2. Click New Modifier .
3. In the drop-down list that appears, scroll down and double-click Sequence. This indicates that the book element can include one or more elements.
4. Click New Reference to Element .
5. Type title in the entry field and press Enter.

Because the reference to the title element appears immediately after the Sequence modifier, the DTD editor assumes that the default behavior is what is wanted. That is, the book element must contain exactly one instance of the title element.

6. Click the Sequence modifier.
7. Click New Modifier .
8. Double-click One or More. (There can be one or more author elements in each book element.)
9. Click New Reference to Element .
10. Type author in the entry field and press Enter.

At this point, the definition of the book element is complete, and the tree diagram of bookstore.dtd should look like this:

Figure 66. Early Steps of bookstore.dtd

However, you have not yet specified that you want the book element itself to be optional. You need to do this in the element that references the book element. For example, suppose the bookstore element is the root element in XML documents that use this DTD. Further suppose that you want the book element to be a child of the bookstore element.

You can define the bookstore element as follows:
1. Click the DTD node at the top of the tree.
2. Click New Element Definition .
3. Type bookstore in the entry field and press Enter.
4. Click New Modifier .
5. In the drop-down list that Stylus Studio displays, double-click Optional.
6. Click New Reference to Element .
7. Type book in the entry field and press Enter.
8. Click Save

Examining the Tree of a Sample DTD

This topic is part of a sequence that starts with Creating a Sample DTD.

Your DTD should now look like Figure 67.

i

Figure 67. Finished bookstore.dtd

To complete this DTD, you could define magazine and newsletter elements. In the bookstore element definition, you could add references to the magazine and newsletter elements. You could also expand the definition of the book element to include information about the publisher, price, publication date, and number of pages.

 
Free Stylus Studio XML Training: