Defining complexTypes That Contain Elements and Attributes - Diagram View

To define a complex type in the Diagram view:
1. Right-click the schema node .
2. In the shortcut menu, select Add > ComplexType.

Alternatives: This operation is also available from the XMLSchema > Diagram > Add > ComplexType menu and from the Add button .

The new complexType is added to the XML Schema. It is displayed in the diagram and in the text pane (if you have it open). The properties for the new complexType are displayed in the Properties window.

Adding Nodes to a complexType

Once you have created a complexType, you can further define it by adding sequences, elements, and other nodes. The basic procedure for adding nodes to a complexType is to:

1. Select the node.
2. Use the menus or tool bar to add the node.
3. Fully describe the complexType and its nodes by editing values in the Properties window.

You can use this procedure to add the following nodes to a complexType:

  • all
  • annotation
  • anyAttribute
  • attribute
  • attributeGroup
  • choice
  • group
  • sequence

Next steps vary according to the constraints on the elements in the complexType. The following instructions show how to achieve some typical constraints.

Choosing an Element

Suppose you want to define a complexType that contains exactly one element, and that element can be one of several different elements. In XML Schema, you do this by defining xsd:choice.

To define xsd:choice in the Diagram tab:
1. Right-click the icon that represents your new complexType.
2. In the shortcut menu that appears, select Add > Choice.

Alternatives: This operation is also available from the XMLSchema > Diagram > Add > Choice menu and from the Add button .

Stylus Studio displays the choice icon alongside the complexType icon.

3. Right-click the choice icon and select Add > Element, or use the Add button .

A element is added to the choice icon.

Figure 278. Defining Choice for a complexType

4. Make sure the new element is selected. In the Properties window, click the Type field.
5. Enter or select the type of the element.
6. Repeat step 3 through step 5 for each element that might be in the complexType.

Including All Elements

Suppose you want to define a complexType that contains a number of elements, the elements can be in any order, and there must be zero or one of each element. In XML Schema, you do this by defining xsd:all.

To define xsd:all in the Diagram tab:
1. Right-click the icon that represents your new complexType.
2. In the shortcut menu that appears, select Add > All.

Alternatives: This operation is also available from the XMLSchema > Diagram > Add > All menu and from the Add button .

Stylus Studio displays the all icon alongside the complexType icon.

3. Right-click the choice icon and select Add > Element, or use the Add button .

A element is added to the all icon.

4. Make sure the new element is selected. In the Properties window, click the Type field.
5. Enter or select the type of the element.
6. If the element is required, go to step 7. If the element is optional, click the Min Occur. field in the Properties window, and type a zero (0).
7. If there must always be exactly one of this element, go to step 8. If there can be more than one of this element, click the Max Occur. field in the Properties window, and enter the maximum number allowed or click unbounded in the drop-down list.
8. Repeat step 3 through step 7 for each element that can be in the complexType.

Specifying the Sequence of Elements

Suppose you want to define a complexType that contains a number of elements in a particular order. The default is that each element must appear exactly once. However, some elements are optional, and some elements can appear more than once. In XML Schema, you do this by defining xsd:sequence.

To define xsd:sequence in the Diagram tab:
1. Right-click the icon that represents your new complexType.
2. In the shortcut menu that appears, select Add > Sequence.

Alternatives: This operation is also available from the XMLSchema > Diagram > Add > Sequence menu and from the Add button .

Stylus Studio displays the sequence icon alongside the complexType icon.

3. Right-click the sequence icon and select Add > Element, or use the Add button .

A element is added to the sequence icon.

4. Make sure the new element is selected. In the Properties window, click the Type field.
5. Enter or select the type of the element.
6. If the element is required, go to step 7. If the element is optional, click the Min Occur. field in the Properties window, and type a zero (0).
7. If there must always be exactly one of this element, go to step 8. If there can be more than one of this element, click the Max Occur. field in the Properties window, and enter the maximum number allowed or click unbounded in the drop-down list.
8. Repeat step 3 through step 7 for each element that can be in the complexType.

Reordering Nodes

If you make a mistake in the order in which you specify nodes in your XML Schema (when specifying elements in a sequence, for example), you can rearrange them.

To reorder nodes in the diagram view:
1. Click the node you want to move.
2. Click the Move Up or Move Down from the Stylus Studio tool bar until the node is positioned where you want it.

Alternative: This operation is also available from the XMLSchema menu and from the node's shortcut menu.

Combining the Sequence and Choice Modifiers

Suppose you want to define a complexType that contains a number of elements in a particular order, but some of them are optional, and you want to ensure that only one element from a particular group of elements is present. In other words, you need to combine the use of the sequence and choice modifiers. To define this, you must define a sequence modifier first. You can then define sequence and choice modifiers that are children of the initial sequence modifier.

 
Free Stylus Studio XML Training: