Defining an XML Schema Using the Diagram Tab - Getting Started

This section provides a quick tour of the main features of the Diagram tab of the XML Schema Editor and shows you how to define a simple XML Schema. For complete documentation about how to use the XML Schema Editor, see Creating an XML Schema in Stylus Studio.

The topics in this section provide step-by-step instructions for defining the bookstoreDiagram.xsd XML Schema document. 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 will need to start Stylus Studio if you have not already. See Starting Stylus Studio.

Introduction to the Diagram Tab

The recommended way to define an XML Schema in Stylus Studio is to start with the Diagram tab of the XML Schema Editor, which is shown in Figure 68.

Figure 68. Diagram Tab of the XML Schema Editor

When you use the Diagram tab to define an XML Schema, you can create XML Schema nodes directly on the XML Schema diagram pane using tools on the tool bar or from the XML Schema > Diagram and shortcut menus. You can also type in the text pane, which appears under the Diagram tab. The text pane displays the XML Schema syntax Stylus Studio creates for you as you work in the diagram pane.

Stylus Studio ensures that the XML Schema you create is valid. For example, any nodes you define are created in the required order in the XML document that contains the XML Schema definition, regardless of the order in which you create them.

The Diagram tab, shown in Figure 68, consists of three main areas:

This section describes these areas and how to work with them.

Diagram Pane

The diagram pane contains graphical representations of the elements, attributes, and other nodes that make up your XML Schema.

Figure 69. Schema Diagram Pane

Nodes

Each node displayed in the diagram pane is represented by its own symbol; tool tips, which are displayed when you hover over a node in the diagram, identify the node's type (element, attribute, sequence, and so on). The symbols used in the diagram are summarized in Table 1.

Symbol
Meaning
schema (xsd:schema)
annotation (xsd:annotation)
documentation (xsd:documentation)
element (xsd:element)
attribute (xsd:attribute)
attributeGroup (xsd:attributeGroup)
simpleType (xsd:simpleType)
complexType (xsd:complexType)
choice (xsd:choice)
sequence (xsd:sequence)
key (xsd:key)
key reference (xsd:keyref)
unique (xsd:unique)
group (xsd:group)
simpleContent (xsd:simpleContent)
complexContent (xsd:complexContent)
restriction (xsd:restriction)
extension (xsd:extension)
union (xsd:union)
list (xsd:list)
Table 1. Symbols Used in the XML Schema Diagram

Nodes can be expanded and collapsed using the plus and minus symbols, respectively, that appear on the right side of the node. In Figure 72 for example, the PurchaseOrderType complexType has been expanded. The shipTo element has not.

Displaying Properties

To streamline the diagram, most nodes are displayed with their properties hidden by default. Exceptions include element, extension, and restriction nodes, for which the type is displayed, as shown in the productName element in Figure 70.

Figure 70. Most Nodes Appear with Properties Hidden

You can change the display for classes of nodes (all elements, for example) using the Schema Diagram Properties dialog box, shown in . (In addition, the Properties window displays all the properties for any node you select.)

Figure 71. Schema Diagram Properties Dialog Box

For each node property, you can choose to

  • Show the property
  • Show the property only if it is not empty
  • Hide the node

If all of a node's properties have the same show/hide setting, that value is displayed in the Inline Visibility in Diagram field. If no value is displayed in the Inline Visibility in Diagram field, it means that two or more properties have different show/hide settings.

To display the Schema Diagram Properties dialog box:
  • Select Diagram > Properties from the Stylus Studio menu
  • Select Properties from the diagram shortcut meu

Tip

 

You can also change schema diagram properties on the Diagram page of the Options dialog box - Tools > Options > Module Settings > XML Schema Editor > Diagram.

To change node properties display:
1. Display the Schema Diagram Properties dialog box, or the Diagram page of the Options dialog box.
2. Select the node whose properties display you want to change.

Tip

 

To hide all properties, click the Hide All button. To restore defaults, click the Restore Defaults button.

3. Click OK.
Background color

Background color is used as another visual cue for information about the XML Schema:

  • A tan, or light brown, color identifies global nodes - these are elements, types, and so on, that are defined as children of the schema ( xsd:schema). In Figure 72, the purchaseOrder element is an example of a globally defined node.
  • A light yellow background identifies local instances of globally defined types. In Figure 72, the PurchaseOrderType complexType is a local instance of that type.

Figure 72. Background Colors Show Global and Local Types

Moving around the diagram

There are several ways to move around the diagram pane:

  • To move from node to node in the diagram, press the arrow keys.
  • You can use the scroll bars to explore the diagram; the zoom slider lets you change the magnification.
  • Click Go to Definition ( ) on the shortcut menu to display a new page that shows just the type definition.
  • Click Display Definition ( ) on the shortcut menu to jump to the place in the XML Schema where the type is defined.

Text Pane

The text pane appears directly beneath the XML schema diagram pane. It displays the XML Schema code represented by the nodes you create in the diagram. The default font is Courier New, but you can change it to whatever font you want by clicking the Change Font button ( ).

Stylus Studio synchronizes the diagram and text views of the XML Schema - any changes you make in the diagram are reflected in the text pane, and vice versa. Synchronization information is displayed in the bar that separates the diagram and text panes. Current status is displayed on the right. When the two views are synchronized, Stylus Studio displays this graphic: . When Stylus Studio detects a change, such as a change to the text, it displays a message and changes the status graphic, as shown in Figure 73.

Figure 73. Synchronization Status is Monitored and Displayed

You can use the splitter to resize the text pane to view more or less text, or you can hide it entirely using the controls on the splitter's right side.

Figure 74. Splitter Controls Change Size of Text and Diagram Panes

Stylus Studio supports backmapping between the text pane and the XML Schema diagram pane - if you click a node in the diagram, Stylus Studio scrolls the text pane to display the line of XML Schema that defines the node you clicked. A blue triangle is displayed to the left of the exact line of code.

Definition Browser

The definition browser is a drop-down list that displays all the child nodes of the schema node. It is located at the top of the Diagram tab.

Figure 75. Definition Browser

When you select a node from the definition browser, Stylus Studio displays a new page in the XML Schema diagram pane that shows the definition of the node you select. In addition, the definition browser displays information about that node.

Figure 76. Information About the Selected Node

To return to the page you were viewing previously, press the back ( ) button.

Note

 

When you display a node using the definition browser, the focus of the text pane does not change. Clicking the node jumps you to that part of the XML Schema where the node is defined.

Editing Tools of the Diagram Tab

Many of the operations you perform in the Diagram tab can be performed in a number of ways. This section briefly describes menu and tool bar use, and introduces additional features for defining XML Schema.

In this section

This section covers the following topics:

Menus and Tool Bars

The complete set of available operations is defined by the menu system. The tool bar provides a subset of frequently performed operations. The top-level menu ( XMLSchema > Diagram), the shortcut menus, and the tool bar are context sensitive - only operations that are permitted given the current context are available. For example, if you want to add an element to a sequence, you can

  • Select XML Schema > Diagram > Add > Element from the main menu
  • Select Add > Element from the sequence shortcut menu
  • Click the Add button on the tool bar and select Element from the drop-down list it displays

Each of these actions lets you add a new node, in this case, an element, to your XML Schema definition.

In-place Editing

In-place editing allows you to change node names and properties directly in the diagram. For example, say you want to change the value of the Mixed property of the PurchaseOrderType complexType. Just double-click the property. Stylus Studio opens the property for editing, as shown in Figure 77.

Figure 77. In-Place Editing

Similarly, if you double-click a node name, Stylus Studio places the property in edit mode, allowing you to type a new name.

Tip

 

To display all of a node's properties in the diagram, see Displaying Properties.

Drag-and-Drop

An alternative to using the menu and the tool bar is to use drag-and-drop, which lets you add an existing node to another node's definition. For example, say you wanted to add an existing element to a sequence. You can do this by dragging the element icon to the sequence icon, as shown in Figure 78.

Figure 78. Using Drag-and-Drop to Define a Node

Use drag-and-drop any time you want to define a node using a node already defined in your XML Schema.

Tip

 

When you drag and drop, you remove the element from its current context. If you want to make a copy an element, press and hold the CTRL key when you perform the drag operation.

Typical targets of drag-and-drop operations include the following nodes

  • schema
  • sequence
  • choice
  • all
  • list
  • annotation
  • restriction
  • union

Typical sources for drag-and-drop operations include the following nodes

  • simpleType
  • element
  • annotation

Tip

 

Any node you drag to the schema node is created as a child of the schema node.

QuickEdit

QuickEdit is a feature of the Diagram tab that streamlines common editing operations. For example, you can use QuickEdit to

  • Change a sequence to a choice or to an all
  • Specify a restriction for a simpleType
  • Create sequence, choice, any, and all element definitions

For example, the following structure was created by selecting QuickEdit > Add Elements Choice from the complexType's shortcut menu.

Figure 79. QuickEdit Creates Complex Definitions With a Click

QuickEdit appears on the top-level and shortcut menus in those contexts in which it is available, and it is also available on the tool bar by pressing the QuickEdit button .

Refactoring

Refactoring is a process that allows you to copy globally defined nodes from one XML Schema and paste them in a new XML Schema. The difference between refactoring and a simple copy is that refactoring includes both the node you select and all its dependencies. Consider the following example: here is how the purchaseOrder node appears when it is copied from purchaseOrder.xsd and pasted into a new XML Schema document:

Figure 80. Simple Copy/Paste of a Node

As you can see, the copy action copies only the selected node to the clipboard. When the same node is copied using refactoring and pasted into another XML Schema document, the node, and all its dependencies copied as well.

Figure 81. Refactoring Copy/Paste of a Node

Not all of the diagram or text are displayed in this illustration, but it is clear that more than just the purchaseOrder node was copied to the clipboard. For example, the purchaseOrder's type, PurchaseOrderType complexType has been copied, as well as PurchaseOrderType's element and sequence nodes, such as shipTo, billTo, and items.

If you were to scroll up either the text pane or the diagram pane, you would also see, for example, the complete definitions for other global complexTypes such as SKU and USAddress.

To refactor a node:
1. Right-click the node you want to refactor.
2. Select Refactoring > Copy from the node's shortcut menu.

Note

 

If the node is not globally defined, refactoring is not available.

The node and all its dependencies are copied to the clipboard.

3. To paste the node in the target XML Schema document, select Refactoring > Paste from the shortcut menu.

Description of Sample XML Schema

Suppose you want to define an XML Schema that defines book, magazine, and newsletter elements. The type of each of these elements is PublicationType. The XML Schema defines the PublicationType complexType. An element that is a PublicationType has the following description:

  • The genre attribute specifies the style of the publication. That is, whether it is a book, magazine, or newsletter.
  • There is always exactly one title element.
  • The subtitle element is optional.
  • There must be at least one author element and there can be more. Each author element contains one first-name element and one last-name element.
  • Of the following three elements, exactly one must always be present:
    • ISBNnumber
    • PUBnumber
    • LOCnumber
  • The elements must be in the order specified in this list.

The following topics in this section describe how to define this XML Schema using the Diagram tab of the XML Schema Editor.

Defining a complexType in a Sample XML Schema in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

The steps for defining the PublicationType complexType described in Description of Sample XML Schema are presented in the following topics:

Defining the Name of a Sample complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

To define a complexType in a sample XML Schema:
1. From the Stylus Studio menu bar, select File > New > XML Schema.

Stylus Studio displays the XML Schema Editor. Maximize the XML Schema Editor window. If the Project window is visible, you can close it.

2. At the bottom of the XML Schema editor, click the Diagram tab.

Stylus Studio displays the Diagram view for the new schema.

Figure 82. The XMLSchema Editor Diagram Tab

3. Right-click the schema node in the XML Schema diagram pane and select Add > ComplexType from the shortcut menu.

Alternatives: This action is also available from the XMLSchema > Grid Editing menu.

Stylus Studio displays a representation for the new node in the diagram. The complexType properties appear in the Properties window. The new complexType has a default name of ComplexType-0.

Figure 83. New complexType

4. Type PublicationType in the Name property in the Properties window and press Enter.

Stylus Studio updates the diagram and the XML Schema in the text pane.

5. Click Save .
6. In the Save As dialog box, in the URL field, type bookstoreDiagram.xsd, and click Save. You can save it in the examples directory of the Stylus Studio installation directory or in a directory of your choice.

Adding an Attribute to a Sample complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

To add the genre attribute to the PublicationType complexType:
1. Right-click the PublicationType node.
2. In the shortcut menu that appears, select Add > Attribute.

Stylus Studio displays a node for the new attribute ( untitled).

Figure 84. Adding an Attribute in the Diagram Tab

3. In the Properties window, type genre as the name of the new attribute and press Enter.
4. In the Properties window, click the Type field.

Stylus Studio displays a drop-down list of built-in types.

5. Scroll down to xsd:string and click it, or type xsd:string and press Enter.

The diagram should now look like the one shown in Figure 85

Figure 85. The Finished genre Attribute

.

Tip

 

You can toggle the display of attributes by clicking the small triangle at the bottom of the complexType node.

6. Click Save .

Adding Elements to a Sample complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

The elements belonging to this complexType must occur in a specific order. Before defining the first element, you need to create a sequence node to define this requirement in the XML Schema.

To add the title element to the PublicationType complexType:
1. Right-click the PublicationType node.
2. In the shortcut menu that appears, select Add > Sequence.

The sequence node is added to bookstoreDiagram.xsd. The sequence modifier indicates that if an instance document contains the sequence node's child elements (the elements you will add next), they must be in the order in which they are defined.

3. Type title and press Enter.
4. Right-click the sequence node .
5. In the shortcut menu that appears, select Add > Element.

A child element is added to the PublicationType node.

6. In the Properties window, click the Name field and enter title.
7. In the Properties window, click the Type field.

Stylus Studio displays a drop-down list of built-in types.

8. Scroll down to xsd:string and click it, or type xsd:string and press Enter.

According to the XML Schema requirements described earlier, the title element can occur only once. By default, the default value for the Min Occur. (minimum occurrences) and Max Occur. (maximum occurrences) properties is 1. You want exactly one instance of the title element in PublicationType, so you can accept these defaults.

Adding Optional Elements to a Sample complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

To add the optional subtitle element to the PublicationType complexType:
1. Right-click the sequence node .
2. In the shortcut menu that appears, select Add > Element.

Below the title element, Stylus Studio displays a rectangle for the new element definition.

3. Rename the new element subtitle.
4. Give the new element a data type of xsd:string.
5. Give the new element a minimum occurrences value of 0.

You can accept the default of 1 for the Max Occur. property.

6. Click Save .

At this point, the XML Schema diagram should look like Figure 86:

Figure 86. PublicationType complexType

Adding an Element That Contains Subelements to a complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

The sample schema requirements (see Description of Sample XML Schema) state that the PublicationType complexType must include at least one author element. Further, an author element must include a first-name element and a last-name element.

Each element that can contain one or more subelements is a complexType. Consequently, to add the author element to the PublicationType complexType, you must first define the AuthorType complexType. You can then add an element that is of AuthorType to the PublicationType complexType.

To define the AuthorType complexType:
1. Right-click the schema node in the XML Schema diagram pane and select Add > Complex Type from the shortcut menu.

Alternatives: This action is also available from the XMLSchema > Grid Editing menu.

Stylus Studio displays a representation for the new node in the diagram. The complexType properties appear in the Properties window.

2. Type AuthorType in the Name property in the Properties window and press Enter.

Stylus Studio updates the diagram and the XML Schema in the text pane.

3. Right-click the AuthorType node in the diagram.
4. In the shortcut menu that appears, select Add > Sequence.

Stylus Studio displays the sequence node .

5. Right-click the sequence node.
6. In the shortcut menu that appears, select Add > Element.
7. Type first-name in the Name property in the Properties window and press Enter.
8. Change the Type property to xsd:string and press Enter.
9. Repeat step 5 through step 8 to add a new element to the sequence, using last-name as the name of the new element.
Now you can add the author element to the PublicationType complexType:
1. Right-click the sequence node belonging to the PublicationType node.
2. In the shortcut menu that appears, select Add > Element.

Stylus Studio displays a representation for the new node in the diagram. The complexType properties appear in the Properties window.

3. Type author in the Name property in the Properties window and press Enter.

Stylus Studio updates the diagram and the XML Schema in the text pane.

4. Click the Type field in the Properties window. Stylus Studio displays a drop-down list of built-in types plus any types you have defined, such as the AuthorType you defined in the previous procedure.
5. Select AuthorType from the drop-down list.

Tip

 

A plus sign appears on the right side of the author element. You can click the plus sign to display the definition of the AuthorType complexType, which was just added to the author element.

6. Click the Max Occur. field.
7. In the drop-down list that appears, click unbounded.

Tip

 

When you give an element an unbounded maximum number of occurrences, Stylus Studio renders the node using two outlines, to indicate that multiple occurrences of this element are allowed.

8. Click Save .

At this point, the XML Schema diagram should look like Figure 87:

Figure 87. author Element with AuthorType complexType

Choosing the Element to Include in a Sample complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

In the sample XML Schema, you want PublicationType elements to contain an ISBNnumber, PUBnumber, or LOCnumber element.

To specify this:
1. Right-click the sequence node belonging to the PublicationType node.
2. In the shortcut menu that appears, select Add > Sqeuence.

Stylus Studio displays a representation for the new sequence node in the diagram. Sequence properties appear in the Properties window.

We added the sequence node in error - the specification requires that this node be a choice node. The QuickEdit feature makes it easy to correct errors such as this.

3. Right-click the new sequence node. In the shortcut menu that appears, select QuickEdit > Switch to Choice.

Stylus Studio changes the sequence node to the choice node ( ).

4. Right-click the new choice node.
5. In the shortcut menu that appears, select Add > Element.
6. In the Properties window, change the Name to ISBNnumber and press Enter.
7. In the Properties window, change the Type xsd:int and press Enter.
8. Repeat step 4 through step 7 twice: once to add the PUBnumber element, and once to add the LOCnumber element.
9. Click Save .

The definition of the PublicationType complexType is now complete and should look like Figure 88:

Figure 88. PublicationType complexType Fully Defined

Defining Elements of the Sample complexType in the Diagram View

This topic is part of a sequence that begins with Description of Sample XML Schema.

In the final step of defining bookstoreDiagram.xsd, you define elements that are of the PublicationType complexTypes you defined earlier - book, magazine, and newsletter elements.

To define the book, magazine, and newsletter elements in the sample XML Schema:
1. Right-click the schema node in the diagram.
2. In the shortcut menu that appears, select Add > Element.

Stylus Studio displays a node for the new element in the XML Schema diagram pane. The properties for the new element appear in the Properties window.

3. Type book as the name of the new element and press Enter.
4. In the Properties window, click the Data Type field. Stylus Studio displays a drop-down list of built-in types plus any types you have defined.
5. Click PublicationType.
6. Repeat step 1 through step 5 twice: once to add the magazine element, and once to add the newsletter element.
7. Click Save .

The bookstoreDiagram.xsd document is now complete.

8. Select XMLSchema > Validate Document from the menu to validate the XML Schema document you created.

The validation message appears in the Output window, as shown in

Figure 89. Validation of bookstoreDiagram.xsd

 
Free Stylus Studio XML Training: