XML Schema Editor Tutorial
This tutorial will show you how to create a XML Schema (XSD) using the
XML Schema Editor. We will create a new XSD for a Purchase Order. Before
you begin this tutorial, you might want to review the XML
Schema Part 0: Primer to learn about XML Schema. The Purchase Order
we create below will contain the similar information as the first example
that is specified in the Primer.
Setup
Before you start this tutorial, make sure you have created a solution and
a project. In the following example, we assume the B2B/MyProject project
is created. Also, under Perspective, switch to the XML Perspective. The
completed example is located in plugins/xsdeditor/samples/PurchaseOrder/PurchaseOrder.xsd.
Getting Started
From the Workbench, choose New->XML Schema. The Create XSD Schema
wizard will come up. Specify an XSD name, such as PurchaseOrder.xsd.
When you say Finish, a PurchaseOrder.xsd resource will be created under
B2B/MyProject in the Navigator view. You can double click on the PurchaseOrder.xsd
to open the XML Schema Editor.
The Content Outline view will show you all the items in your XSD as
you add them. You will notice the PurchaseOrder file object. The
right hand side of the window is a detailed Design View and Source View
for the XSD.
Creating the structure of the XSD
Adding Annotation
We will first add a comment to the Purchase Order schema:
-
Mouse button 2 on the PurchaseOrder file object and select Add
Annotation. This will create a new annotation element and a new documentation
element for comment.
Adding Global Elements
Next we will add two global elements to the Purchase Order schema:
-
Mouse button 2 on the PurchaseOrder file object and select Add
Global Element. This will create a new global element called New_GlobalElement1.
Change its name to purchaseOrder in the Design View.
-
Repeat and add another global element, called comment.
Adding Complex Types
Next we will add three complex types to the Purchase Order schema:
-
Mouse button 2 on the PurchaseOrder file object and select Add Complex
Type. This will create a new complex type called New_ComplexType1.
Change its name to PurchaseOrderType in the Design View.
-
Repeat and add complex type USAddress.
-
Repeat and add complex type Items.
Adding Simple Type
Next we will add a simple type to the Purchase Order schema.
-
Mouse button 2 on the PurchaseOrder file object and select Add Simple
Type. The will create a new simple type called New_SimpleType1.
Change its name to Sku in the Design View.
Filling in the details
Now that we have created the general outline for the XML Schema, we will
go back to each object to fill in the details.
Click on the PurchaseOrder documentation object. In the Comment
data entry field, enter some comments, similar to the Purchase Order example
from the Primer.
Click on the purchaseOrder global element. Change the type to
User-defined
Complex Type, and select PurchaseOrderType as its type.
Right mouse button 2 on the complex type PurchaseOrderType. You
will notice a number of menu items. We will use it to add the components
to the PurchaseOrderType:
-
Select Add Content Model. This will add a new sequence.
-
Under the sequence icon, select Add Element. This will add a new
element called New_Element1. Changes its name to shipTo in
the Design View. Change the type to
User-defined Complex Type, and
select USAddress as its type.
-
Add element billTo. Also set its type to USAddress.
-
Select Add Element Ref. This will add a new element reference to
a global element. Change the element reference to the global element comment.
-
Add element items. Set its type to Items
You have now completed the element declarations for PurchaseOrderType.
We will now add an attribute declaration.
-
Right mouse button 2 on the complex type PurchaseOrderType.
-
Select Add Attribute. This will add a new attribute called New_Attribute1.
Change its name to orderDate. Change its type to the built-in type
date
instead of the default string.
Right mouse button 2 on the complex type USAddress. Similar to PurchaseOrderType,
add in the sub-component for USAddress following the example in
the Primer. The steps are similar to the PurchaseOrderType and therefore
we will not repeat the description.
Right mouse button 2 on the complex type Items. The Items
complex type has a number of interesting aspect. It uses anonymous types,
so we will go through the steps in some detail:
-
Select Add Content Model. This will add a new sequence.
-
Under the sequence icon, Select Add Element to add a new element
item.
In the Design View for item, change the type to a User-defined Complex
Type, and select the combo-box item **anonymous**. This will
cause a new anonymous complex type to be added to item.
-
Right mouse button 2 on this anonymous complex type and select Add
Content Model to add a new sequence.
-
Under the sequence icon, select Add Element to add a new element
called productName. Repeat, and add another element called quantity.
-
In the Design View for quantity, change the type to a User-defined
Simple Type, and select the combo-box item **anonymous**. This
will cause a new anonymous simple type to be added to quantity.
-
Right mouse button 2 on this simple type and select Add Restriction.
In the Design View for this restriction element, set its base type to positiveInteger,
and the Maximum Exclusive field to 100
-
Add the remaining element declarations, element reference, and attribute
declaration such as USPrice, comment, shipDate, and partNum to the
anonymous complex type.
Click on the simple type Sku. We want to derive its type from a
built-in type and apply constrain to its value.
-
Right mouse button 2 on the simple type Sku. Select Add Restriction.
This will add a new restriction element. In the Design View, change the
base type to Built-in Simple Type and select a string.
-
Right mouse button 2 on the restriction element and select Add Pattern.
This will add a new pattern called New_Pattern1. Change its value
to \d{3}-[A-Z]{2} in the Design View.
We have now completed our Purchase Order schema. Your XML Schema outline
looks something like the screen capture below. You can choose Validate
XML Schema to validate this XSD against the W3C schema dtds.
Viewing the XSD Source
At any time, you can switch to the Source tab to review the source for
the XML Schema.
Saving the XSD Model
At any point in time, you can save the XSD. Select File->Save PurchaseOrder.xsd
from the file menu to save it.