Model XML DTDs with Rational Rose

You can model your XML documents in much the same way you would your components and modules when you use the Rational Rose development tools. Here's how.

This article originally appeared as an XML e-newsletter.

By Brian Schaffner

Many developers are familiar with object-oriented analysis and design tools. These tools are often employed to assist in the development of data and component models used to architect large software systems. For developers using the Rational Rose development tools, it may come as a surprise that you can model your XML documents in much the same way you would your components and modules.

Basic concepts

In order to start creating XML models in Rational Rose, there are a few basic concepts you'll need to understand. First, each node within your document will become a class. There are four primary types of XML classes you can use: element, entity, group, and notation. For our purposes, we'll only use elements and groups.

An element is a basic class that can have certain characteristics, which are defined by the type you specify in Rational Rose. For elements, you can select ANY, Content Model, PCDATA, or EMPTY. An EMPTY element is obviously empty, and a PCDATA type is a character data container. For mode complex types, you use Content Model.

Content Model and groups

For simple elements that contain character data, you'll use the PCDATA type. However, many of your elements will contain one or more child elements. For these complex elements, you must use the Content Model type.

In order to specify to Rational Rose the relationships between elements, you'll also have to use group types. Essentially, each Content Model element will have an associated group element. The group element should be created as a nested class in Rational Rose. Each element that is a child of the Content Model element will be defined by associating it with the Content Model element's group element.

A quick walk-through

Let's walk through a short example. Start by creating a new Rational Unified Process (RUP) project in Rational Rose. You should see the Welcome Class Diagram from the Logical View. You can delete the node within this diagram; it won't be needed.

Click the Class icon on the toolbar and click on the diagram to create a new class. Name the class Order. Double-click the Order class and select the DTDElement stereotype. In the Class Specification dialog box, select the Content Model type.

In the tree view, right-click on the Order class and select New | Nested Class. Name the new class Order_grp. Click and drag the Order_grp class onto the class diagram. Double-click the Order_grp class and select the DTDGroup stereotype. The grouping type should be Sequence and the occurrence should be One. Next, expand the Associations item in the tree view and drag the Order_grp association onto the class diagram.

Create two new classes called Item and Customer, respectively. Both classes should be DTDElements defined as Content Model types. Create a new association from the Order_grp group element to the Item class, and repeat for the Customer class.

Double-click the new association for the Item class and name it Item. Then click the Role A Detail tab and select 1.n in the multiplicity drop-down box. This specifies that there will be one or more Item elements as children to the Order element.

Double-click the association for the Customer class and name it Customer. Click on the Role A Detail tab and select 1 in the multiplicity drop-down box. This specifies that there will only be one Customer element as a child to the Order element.

You can repeat this process as necessary to complete the model of your XML DTD. For each complex element, simply specify the Content Model type and then create an associated nested class with the DTDGroup stereotype. Your child elements should then be associated to the group element. When you've completed your model, you can right-click on the classes and select XML_DTD | Generate DTD to create the DTD information. You can also browse the DTD source from this menu.

Bottom up

You can also reverse engineer your DTDs into Rational Rose by selecting Tools | XML_DTD | Reverse Engineer XML_DTD. You'll be prompted to locate the existing DTD file. When the processing is complete, your Rational Rose models will contain new packages for your DTD classes.

Brian Schaffner is an associate director for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Technology Consulting practice.

Editor's Picks