A First Look

Contact Us or call 1-877-932-8228
A First Look

A First Look

An XML schema describes the structure of an XML instance document by defining what each element must or may contain. An element is limited by its type. For example, an element of complex type can contain child elements and attributes, whereas a simple-type element can only contain text. The diagram below gives a first look at the types of XML Schema elements.

Note: we will review this in the next presentation.

Schema authors can define their own types or use the built-in types. Throughout this course, we will refer back to this diagram as we learn to define elements. You may want to save this diagram (right-click the image and select "Save Image As..."), so that you can easily reference it.

The following is a high-level overview of schema types.

  1. Elements can be of simple type or complex type.
  2. Simple type elements can only contain text. They cannot have child elements or attributes.
  3. All the built-in types are simple types (e.g., xs:string).
  4. Schema authors can derive simple types by restricting another simple type. For example, an email type could be derived by limiting a string to a specific pattern.
  5. Simple types can be atomic (e.g., strings and integers) or non-atomic (e.g., lists).
  6. Complex-type elements can contain child elements and attributes as well as text.
  7. By default, complex-type elements have complex content, meaning that they have child elements.
  8. Complex-type elements can be limited to having simple content, meaning they only contain text. They are different from simple type elements in that they have attributes.
  9. Complex types can be limited to having no content, meaning they are empty, but they may have attributes.
  10. Complex types may have mixed content - a combination of text and child elements.

A Simple XML Schema

Let's take a look at a simple XML schema, which is made up of one complex-type element with two child simple-type elements.

Code Sample:

SchemaBasics/Demos/Author.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Author">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="FirstName" type="xs:string" />
        <xs:element name="LastName" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

As you can see, an XML schema is an XML document and must follow all the syntax rules of any other XML document; that is, it must be well formed. XML schemas also have to follow the rules defined in the "Schema of schemas," which defines, among other things, the structure of an element and attribute names in an XML schema.

Although it is not required, it is a common practice to use the xsqualifier to identify schema elements and types.

The document element of XML schemas is xs:schema. It takes the attribute xmlns:xs with the value of http://www.w3.org/2001/XMLSchema, indicating that the document should follow the rules of XML Schema. This will be clearer after you learn about namespaces.

In this XML schema, we see a xs:element element within the xs:schema element. xs:element is used to define an element. In this case it defines the element Author as a complex-type element, which contains a sequence of two elements: FirstName and LastName, both of which are of the simple type, string.

Dividing an XML Schema

The example above is a very simple design method, however for larger and more complex schemas, doing it that way can sometimes make it hard to read and maintain.

An alternative method is to divide the schema by first defining all the simple-type elements and attributes, and then referring to them as you create the more complex-type elements using the "ref" attribute. Below is a brief example (using the same schema definitions above):

Code Sample:

SchemaBasics/Demos/Author-Divided.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 	<!-- STEP 1: define the simple-type elements -->
	<xs:element name="FirstName" type="xs:string"/>
	<xs:element name="LastName" type="xs:string"/>
	<xs:element name="Age" type="xs:integer"/>
	
    <!-- STEP 2: define the attributes -->
	<xs:attribute name="Title" type="xs:string"/>
	
    <!-- 
    	STEP 3: define the complex-type elements referring to the 
        already defined elements and attributes above 
    -->
	<xs:element name="Author">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="FirstName"/>
				<xs:element ref="LastName"/>
				<xs:element ref="Age"/>
			</xs:sequence>
			<xs:attribute ref="Title" use="optional"/>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next