1. Computing & Technology

Discuss in my forum

Defining Complex Types

Writing an XML Schema

By , About.com Guide

A complex type in an XML Schema is an XML element that may have other XML elements within it. In HTML, an example of a complex type is the <body> tag. The body element can have within it many other elements, such as <p>, <table>, or <ol>.

The first complex type we find in the newsletter XML is NewsletterType. To define a complex type in a schema you use the complexType element:

<xsd:complexType name="NewsletterType">

Then you define the elements and attributes of that complex type. Because this type is the primary element of the newsletter, it contains all the different sub elements:

 <xsd:complexType name="NewsletterType">
 <xsd:sequence>
 <xsd:element ref="comment" minOccurs="0"/>
 <xsd:element name="header" type="HeaderType"/>
 <xsd:element name="section1" type="SectionType"/>
 <xsd:element name="section2" type="SectionType"/>
 <xsd:element name="section3" type="SectionType"/>
 <xsd:element name="section4" type="SectionType"/>
 </xsd:sequence>
 <xsd:attribute name="volume" type="xsd:positiveInteger"/>
 <xsd:attribute name="number" type="xsd:positiveInteger"/>
 </xsd:complexType> 

From this listing, we know that there may be zero or more comments, a header field, and four sections. Plus there are two attributes of the NewsletterType: volume and number. We also know that the six sections of the newsletter will come in the following order:

  1. comment (optional)
  2. header
  3. section1
  4. section2
  5. section3
  6. section4

This declaration is a complex type that contains complex type definitions:

  • HeaderType
  • SectionType

But you can create a complex type that only includes elements with simple type definitions. For example, the HeaderType definition looks like this:

 <xsd:complexType name="HeaderType">
 <xsd:sequence>
 <xsd:element name="long_title" type="xsd:string"/>
 <xsd:element name="filename" type="xsd:string"/>
 <xsd:element name="date" type="xsd:date"/>
 <xsd:element name="meta_title" type="xsd:string"/>
 <xsd:element name="meta_description" type="xsd:string"/>
 <xsd:element name="meta_keywords" type="xsd:string"/>
 </xsd:sequence>
 </xsd:complexType> 

As with the NewsletterType, the HeaderType definition has a list of elements that will be found in a specific order. They can contain either character strings (xsd:string) or dates (xsd:date). They are:

  • long_title
  • filename
  • date
  • meta_title
  • meta_description
  • meta_keywords

For most of the above definitions, I have declared a new type definition, but for the comment element, I simply refer to the existing element.

 <xsd:element ref="comment" minOccurs="0"/> 

This allows me to put the comment element in the document where it makes sense to put it. I could even include it in several different complex types. When it occurs above, I have set it to be optional with the minOccurs attribute set to 0. I could also set an upper limit on the number of comments in an element with the maxOccurs attribute.

There are many other ways to create your own types within your schema, this is just a sampling of how it works.

Once you have your simple and complex types defined, you can create your entire schema. Here's what I wrote for the newsletter XML schema.

Next page > Sample XML > Page 1, 2, 3, 4, 5

Previous Features

©2012 About.com. All rights reserved.

A part of The New York Times Company.