WebReference.com - Part 1 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (3/5) | WebReference

WebReference.com - Part 1 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (3/5)

To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Professional XML Schemas

Getting Started with XML Schemas

The best way to start learning the syntax for XML Schemas is to jump in with an example. To start with, we will create a schema for the following simple document:

<?xml version = "1.0" ?>
<Customer>
   <FirstName>Raymond</FirstName>
   <MiddleInitial>G</MiddleInitial>
   <LastName>Bayliss</LastName>
</Customer>

A document conforming to a schema is known as an instance document, so let's have a look at an XML Schema for this instance document; we will go through it line by line in a moment (name the file Customer.xsd):

<?xml version = "1.0" ?>
<schema xmlns = "http://www.w3.org/2001/XMLSchema">
   <element name = "Customer">
      <complexType>
         <sequence>
            <element name = "FirstName" type = "string" />
            <element name = "MiddleInitial" type = "string" />
            <element name = "LastName" type = "string" />
         </sequence>
      </complexType>
   </element>
</schema>

XML Schema files are saved with the .xsd extension.

As you can see, the Customer.xsd schema is itself an XML document, and the root element of any XML Schema document is an element called schema. In the opening schema tag we declare the namespace for the XML Schema Recommendation:

<schema xmlns = "http://www.w3.org/2001/XMLSchema">

The next line indicates how we declare our first element, the Customer element:

   <element name = "Customer">
...
   </element>

As XML is intended to be a self-describing data format, it is hardly surprising that we declare elements using an element called element, and we specify the intended name of the element as a value of an attribute called name. In our case, the root element is called Customer, so we give this as the value of the name attribute.

We will come back to the complexType element that appears on the next line in just a moment, but looking further down the schema we can see the declarations for the three other elements that appear in the document: one called FirstName, one called MiddleInitial, and one called LastName.

         <sequence>
            <element name = "FirstName" type = "string" />
            <element name = "MiddleInitial" type = "string" />
            <element name = "LastName" type = "string" />
         </sequence>

You may be able to guess from the way in which the elements are declared, nested inside an element called sequence, that they would have to appear in that same order in a conforming document. The sequence element is known as a compositor, and we are required to specify a compositor inside the complexType element – we will meet other types of compositor in Chapter 3.

In addition, the element declarations carry a type attribute, whose value is string. XML Schema introduces the ability to declare types such as string, date and integer, as we would find in languages such as SQL and Java; this is how we specify such types.

Let's now come back to the element we have not looked at yet, called complexType, which contains the declarations of the elements that appear as children of the Customer element in our sample XML document. XML Schema makes a distinction between simple types and complex types.


To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Created: October 18, 2001
Revised: October 18, 2001


URL: http://webreference.com/authoring/languages/xml/schemas/chap1/1/3.html