This tutorial will show you how to build the following schema.
The schema contains a number of basic building blocks; elements, attributes & complexTypes. We will see how these can be easily created without needing extensive knowledge of the XSD standard.
Creating a new Schema.
You will now have a blank XSD schema document to work on.
Let's start by adding the entity "authorName" to our blank schema. We can define this as a complexType (A Complex Type is a construct that allows you to build re-usable building blocks. See Schema Tutorial for more information).
A complexType is created by right clicking on the schema element in the graphical view and selecting "Add Child->Complex Type". |

|
You can then enter the name of the complexType into the active edit box on the new ComplexType box. |
 |
You now have a new complexType.
Note : You can edit the name (when the Complex Type is selected) by clicking on the name in the diagram or by changing the name property in the properties window.
|
 |
Before you can add child elements to the complex Type you must define the type of compositor (choice, sequence or all).
You can do this by right clicking on the new complex type and selecting the menu "Add Child Item -> (sequence/choice/all)".
The type of compositor you chose changes the behavior of the element within the XML - for more information see the XSD tutorial |


|
| We can now add child elements. We do this by adding them to the compositor (in this case a sequence). You can do this by right clicking on the sequence and selecting the menu "Add Child Item->Element". |
 |
The element must be given a name. This can be done initially by typing into the active edit box.
Note : You can edit the name (when the Element Type is selected) by clicking on the name in the diagram or by changing the name property in the properties window. |


|
If the element is to contain data then you must select the type of data it can contain. This can be done by clicking on the "type" section in the right hand side of the element box.
Note The list will contain all the predefined simple types, any simple types you have already defined, and all complex types you have defined. |


|
| We can repeat this process and add Surname to the authorNameType. |
 |
The resulting XSD looks like this.
<?xml version="1.0" encoding="utf-16"?> <!-- Created with Liquid XML Studio 0.9.8.0 (http://www.liquid-technologies.com) --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="AuthorNameType"> <xs:sequence> <xs:element name="Forename" type="xs:string" /> <xs:element name="Surname" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> |
Creating the BookType Complex Type
|
| We can use the same techniques seen above to create a second complex Type called BookType. We can also give it a compositor (sequence) and a child element Title of type string. |
 |
We will show the detail of adding the Author Element, as uses the complexType AuthorNameType that we created earlier.
We start by creating and naming our new element. |
 |
We must now set its type. Note that the type list should now contain the complexType AuthorNameType we created earlier.
Note : This method allows you to re-use pre-build blocks throughout your schema. |
 |
Note
Because the type we have selected is a complexType, the contents of the complex type are shown within its own box. |
 |
We can add the child element Genre in the same way as the previous value based elements.
|
 |
However Genre has 2 significant differences.
First, it is optional. We can set this by right clicking on it and setting the cardinality to 0..1
This is change is depicted by showing the cardinality against the element. |


|
Secondly, the value of the element is limited to a set of discreet values - Fantasy, Horror, Thriller, Fiction, Non-Fiction.
We can enforce this by setting the enumeration facet value on the properties window.
Each allowable value is placed on its own line. |


|
The remaining 3 items are attributes.
Attributes can be added to the complexType directly by right clicking on the complexType and selecting the menu "Add Child Item -> Attribute".
You can then name the attribute in the same way as elements and complexTypes. |


|
| The type can then be set in the same way as a element. |
 |
| The remaining attributes can be added in the same way. |
 |
The resulting XML looks like this
<xs:complexType name="BookType"> <xs:sequence> <xs:element name="Title" type="xs:string" /> <xs:element name="Author" type="AuthorNameType" /> <xs:element minOccurs="0" name="Genre"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Fantasy" /> <xs:enumeration value="Horror" /> <xs:enumeration value="Thriller" /> <xs:enumeration value="Fiction" /> <xs:enumeration value="Non-Fiction" /> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> <xs:attribute name="Price" type="xs:decimal" /> <xs:attribute name="PublicationDate" type="xs:date" /> <xs:attribute name="ISBN" type="xs:string" /> </xs:complexType> |
Defining the Main Element
The final job is to define the Root element, BookStore. Only elements defined at the root level (i.e. as children of the schema object), can be used as the root element within an XML document. So an XML schema should always contain at least one root element.
- A root element is not required if the schema is to be included by another schema.
- Only elements that can be used as root elements in an XML document should be defined as root elements in the schema.
|
| A new root element can be added by right clicking on the schema object and selecting the menu "Add Child -> Element" |
 |
| The new element is then named and built up as described previously. |
 |
| We can now add a child element called Book, and set the type to BookType, in the same way as we did previously with AuthorNameType. |

|
Expanded up this shows the full definition for the BookStore element.
The diagram shows that Book is of type BookType. BookType is a complexType, and its contents are shown within the blue box. Changing the contents of this box will alter the definition of BookType. Within BookType you can see Author is of type AuthorNameType, again the contents of this are shown within the nested blue box.
In detail, this diagram shows, the root element BookStore - which means we can have an XML document with the root element <BookStore>. This element can contain 0 to unbounded Book elements. The <Book> element must contain the child elements <Title> and <Author>, and optionally <Genre>. These elements must appear in this order (because they are defined within a sequence). It must also contain the 3 attributes Price, PublicationDate and ISBN. The <Author> element must contain 2 child elements <Forename> and <Surname> which must appear in this order (because they are defined within a sequence).
|
Editing the schema
Once you have defined your schema, it can easily be changed using the graphical environment. If you want to change the order or location of an item, then you simply drag it. You can change the type of a compositor by right clicking on it and setting the new type. The properties of an item can be changed by selecting an item, the applicable properties are then shown in the properties window. |