Defining Elements in DTDs

You can define an element in the Text or Tree tab.

In the Text tab

In the Text tab, you enter the text that defines your element and describes its structure. For example, to define a Catalog element that can contain one or more Publisher elements, followed by zero or more Thread elements, followed by one or more Book elements, you would enter the following:

<!ELEMENT Catalog ((Publisher)+,((Thread)*,(Book)+))>
               

            

When you define elements in the Text tab, you must know the syntax and keywords for what you want to define. This information is publicly available on the World Wide Web. Stylus Studio documentation does not include instructions for defining a DTD in the Text tab. For DTD information, see, http://www.w3.org/TR/REC-xml.

In the Tree tab

When you use Stylus Studio, it is easier to define an element in the Tree tab. In the Tree tab, you click New Element Definition , and Stylus Studio takes care of the syntax and keywords. In the Tree tab, definition of an element requires that you

1. Create the element by specifying its name. To do this, see Defining Elements in the DTD Tree Tab, which is the first topic in this section.
2. Define the structure of the element by specifying modifiers, defining where raw data is allowed, and adding references to other elements. To help you do this, this section discusses the following topics:

Defining Elements in the DTD Tree Tab

In the DTD editor, if the Tree view is not visible, click the Tree tab at the bottom of the window.

To create an element in the Tree tab:
1. Click the DTD node at the top of the tree.
2. In the tool bar on the left, click New Element Definition . Stylus Studio displays a field for the new element at the end of the current contents of the DTD.
3. Type the name of the new element and press Enter. Stylus Studio displays the properties for the new element in the Properties window. If the Properties window is not visible, select View > Properties from the Stylus Studio menu bar. For example, suppose you specified title as the name of the new element. Your new element has the following properties:

Property
Value
Type
Element
Name
title
Content Model
Empty
Table 55. Element Properties

4. To change the value of a property, double-click the current value. For information about the properties that each element can have, see About Node Properties in DTDs.

After you create an element, you must define the structure of the contents of the element. The rest of the topics in this section provide information on how to define structure.

Specifying That an Element Can Have an Attribute in DTDs

In the DTD Tree tab, to specify that an element can have an attribute:
1. Click the name of the element that you want to have an attribute.
2. In the menu bar on the left, click New Attribute .
3. Type the name of the attribute and press Enter.

Specifying That an Element is Required in DTDs

You specify that an element is required when you add a reference to that element in another element.

In the Tree tab, to specify that an element is required:
1. Define the element that you want to be required. See Defining Elements in the DTD Tree Tab.
2. Create the element that contains the element that you want to be required. This is the container element.
3. Click the container element name.
4. In the tool bar on the left, click New Modifier . Stylus Studio displays a drop-down menu.
5. Double-click Sequence.
6. If the required element can appear only once, skip this step. If the required element can appear more than once, click New Modifier and double-click One or More in the pop-up menu.
7. With the modifier highlighted, click New Reference to Element in the tool bar on the left.
8. Enter the name of the element that you want this element to reference.

After you add a reference to an element, you might want to check the definition of the referenced element. To do this, right-click the reference. In the shortcut menu, click Go To Definition. Stylus Studio moves the focus to the definition of the referenced element.

Example 1

For example, suppose the title element is required, and that it is relevant only in the context of a book element. When you define the book element, you specify that it contains the title element. If you specify only the Sequence modifier, the occurrence default is assumed. The occurrence default is that there must be exactly one of the contained element. In other words, the title element is required and there can be only one. In this case, the definition of the book element is as follows:

<!ELEMENT book (title)>
               

            

The tree representation looks like this:

book
               

              
Sequence
                   
title
                   

                

Example 2

It is also possible for an element to be required and for more than one to be allowed. Suppose the book element must also contain at least one author element, but it can contain more than one author element. The definition of the book element is as follows:

<!ELEMENT book (title, author+)>
               

            

The tree representation looks like this:

book
               

              
Sequence
                   
title
                   
One or More
                   
	author
                   

                

Specifying That an Element is Optional in DTDs

You specify that an element is optional when you add a reference to that element in another element. When an element is optional, it means that there can be one or none. If you want to specify that there can be none, one, or more, use the Zero or More modifier. See Specifying That Multiple Instances of An Element Are Allowed in DTDs.

In the Tree tab, to specify that an element is optional:
1. Define the element that you want to be optional. See Defining Elements in the DTD Tree Tab.
2. Create the element that contains the element that you want to be optional. This is the container element.
3. Click the container element name.
4. In the tool bar on the left, click New Modifier . Stylus Studio displays a drop-down menu.
5. If the container element can contain only the optional element, skip this step. If the container element can contain more than one element, click Sequence.
6. Click New Modifier.
7. In the pop-up menu that appears, double-click Optional.
8. In the tool bar on the left, click New Reference to Element and enter the name of the optional element. If the container element can contain additional optional elements, repeat this step for each one.

After you add a reference to an element, you might want to check the definition of the referenced element. To do this, right-click the reference. In the shortcut menu, click Go To Definition. Stylus Studio moves the focus to the definition of the referenced element.

Specifying That Multiple Instances of An Element Are Allowed in DTDs

You specify that multiple instances of an element are allowed when you add a reference to that element in another element. When multiple instances of an element are allowed, you specify that there can be either

  • None, one, or more
  • One or more
In the Tree tab, to specify that there can be multiple instances of an element:
1. Define the element that can appear multiple times. See Defining Elements in the DTD Tree Tab.
2. Define the element that contains the element that can appear multiple times. This is the container element.
3. Click the container element name.
4. In the left tool bar, click New Modifier . Stylus Studio displays a drop-down menu.
5. If the container element can contain only one type of element, skip this step. If the container element can contain more than one type of element, double-click Sequence, and then click New Modifier.
6. Double-click Zero or More to allow the container element to contain zero, one, or more instances of an element. Or, double-click One or More to allow the container element to contain one or more instances of an element.
7. In the left tool bar, click New Reference to Element and enter the name of the element that can appear multiple times. If the container element can contain additional types of elements, repeat this step for each one that can appear multiple times.

After you add a reference to an element, you might want to check the definition of the referenced element. To do this, right-click the reference. In the shortcut menu, click Go To Definition. Stylus Studio moves the focus to the definition of the referenced element.

Example 1

Suppose there are some elements that can appear zero, one, or more times, and there are other elements that can appear one or more times. The tree representation for this might look like the following:

book
               

              
Sequence
                   
One or More
                   
	Author
                   
	Format
                   
Zero or More
                   
	Award
                   
	Review
                   

                

In this example, an instance document must contain these elements in the following order:

author
               
format
               
award
               
review
               

            

Example 2

Suppose you want them in the following order:

review
               
format
               
award
               
author
               

            

In this case, the tree representation would look like this:

book
               

              
Sequence
                   
Zero or More
                   
	Award
                   
One or More
                   
	Format
                   
Zero or More
                   
	Review
                   
One or More
                   
	Author
                   

                

Specifying That An Element Can Contain One of a Group of Elements in DTDs

You might want to define an element that contains one element out of a group of elements. For example, you might want an InventoryNumber element to contain a book, magazine, or newsletter element.

In the Tree tab, to define an element that contains one of a group of elements:
1. Define the elements that your new element can contain. See Defining Elements in DTDs.
2. Define the element that you want to contain another element. This is the container element.
3. Click the container element name.
4. In the left tool bar, click New Modifier . Stylus Studio displays a drop-down menu.
5. Double-click Choice.
6. For each element in the group of elements from which one element can appear:
a. Click New Reference to Element in the left tool bar.
b. Type the name of the element and press Enter.

After you add a reference to an element, you might want to check the definition of the referenced element. To do this, right-click the reference. In the shortcut menu, click Go To Definition. Stylus Studio moves the focus to the definition of the referenced element.

When the XSLT processor validates an instance document against this DTD, it ensures that each instance of the new element you just defined contains exactly one of the referenced elements.

Example

The tree representation for an InventoryNumber element that can contain a book, magazine, or newsletter element would look like the following:

InventoryNumber
               

              
Choice
                   
book
                   
magazine
                   
newsletter
                   

                

Specifying That an Element Can Contain One or More Elements in DTDs

Often, you want an element to contain a sequence of elements. Some of these elements might be required, some might be optional, and some might be able to occur more than once. There might even be a group of elements in which only one can appear.

In the Tree tab, to define an element that contains a sequence of elements:
1. Define the elements that you want your new element to contain. See Defining Elements in DTDs.
2. Define the element that contains the sequence of elements. This is the container element.
3. Click the container element name.
4. In the left tool bar, click New Modifier . Stylus Studio displays a drop-down menu.
5. Double-click Sequence.
6. To add required elements to the container element, click New Reference to Element in the left tool bar and enter the name of the required element. Do this for each required element. You can change the order later.

At this point, you can add

  • Optional elements
  • Elements that can appear one or more times
  • Elements that can appear zero, one, or more times
  • Elements that belong to a group in which only one element in the group can be present
Specifying Modifiers

The procedure is the same for these modifiers. The only difference is the modifier you select. For example, following are the instructions for adding optional elements:

1. In the DTD editor, click the Sequence modifier.
2. In the left tool bar, click New Modifier.
3. Double-click Optional.
4. For each optional element, click New Reference to Element and enter the name of the optional element. This works only if you want all optional elements to be consecutive. If you want optional elements to be interspersed with required elements or elements that can appear one or more times, you must perform steps 1 through 4 for each element.
Modifying the Order

In an instance document, the contained elements must appear in the order in which they are specified in the DTD.

To modify the order:
1. Click the modifier for the element you want to move.
2. Click the up or down arrow repeatedly until the element is where you want it to be.

To move a required element that can appear only once, click its name and then use the up and down arrows.

Alternative: Right-click the item you want to move. Select Move Up or Move Down from the shortcut menu.

Specifying That an Element Can Contain Data in DTDs

To specify that an element can contain raw data, you must first define the element. See Defining Elements in the DTD Tree Tab.

In the Tree tab, to specify that an element can contain data:
1. Click the element you want to contain data.
2. In the left tool bar, click New Modifier . Stylus Studio displays a drop-down menu.
3. Double-click Zero or More.
4. In the left tool bar, click Add $PCDATA .

Moving, Renaming, and Deleting Elements in DTDs

To move an element definition or a reference to an element, in the Tree tab, click the name of the element or the modifier for the reference. Then click Move Up or Move Down repeatedly until the element or reference is where you want it to be.

Alternative: Right-click the item you want to move. Select Move Up or Move Down from the shortcut menu that appears.

Rename

To rename an element or attribute, right-click it and select Rename from the shortcut menu that appears. Type the new name and press Enter.

Alternative : Click Change Name .

Delete

To delete a node in the DTD, right-click the node you want to delete. In the shortcut menu that appears, click Delete.

Alternative : Click Delete Node .

 
Free Stylus Studio XML Training: