Skip Navigation LinksHome : CBC Classes : XML : Schemas : Elements

XML Schema element declarations

  1. Declaring elements - simple type basics
  2. Declaring elements - complex type basics
  3. Declaring elements - multiple children
  4. Defining number of times an element may occur
  5. Defining the child element order

The basics of simple elements

When you declare an element, you first have to decide whether it's simple or complex. A simple element can contain only text or other types of data. A simple element cannot have any attributes or have any children. Any element that has attributes or contains other elements is considered complex.

The most basic declaration for a simple element is:

  <xsd:element name="elementName" type="xsd:type" />

The xsd:type is similar to #PCDATA for a DTD. But unlike #PCDATA the xsd:type can specify exactly what type of data the element can hold. There are many xsd:types (XMLPad will show them all in a code hint), but quite a few have to deal with dates and times, and there are just a handful that are most commonly used:

xsd:string Any characters. (Use xsd:token or xsd:normalizedString if restricting data).
xsd:integer A positive or negative integer number
xsd:decimal A positive or negative number, can also have a decimal part
xsd:float A 32-bit precision positive or negative number. Can also have a decimal part or use scientific notation 123.45E67 (no spaces). Can also have special values INF, -INF or Nan (case sensitive)
xsd:double A 64-bit precision positive or negative number. Same as float, but can be more precise or represent larger numbers.
xsd:boolean Can be set to true or false (case sensitive) or 0 (false) or 1 (true)
xsd:date A date in the format YYYY-MM-DD. It must be in this exact format and the dashes must be included. Can optionally include (+|-)hh:mm
xsd:time A time in the format hh:mm:ss. It must be in this exact format and the colons must be included.

Complete list of xsd:data types


The basics of complex elements

If an element has children, or attributes it is a complex type element. It's not really that complex, but that's how they decided to categorize it. When an element is complex you have to use the xsd:complexType element to declare that it's complex, and then add another element to say whether the children are required or optional. For now we'll use the xsd:sequence element to say the children must appear in the order listed, but there are other options (see below. A simple example of this has a dataroot element named wheel, with a child element of brand:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="wheel">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="brand" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>
</xsd:schema>

Make sure that you note that the "T" in complexType is capitalized. And yes ... that's a lot of code just to declare one child element. Also note that the dataroot element will almost always be complex, that is, in all but the simplest examples it will have children.


Defining multiple children

If an element has more than one child they are all defined inside of the sequence tags. der listed. A simple example of this adds the children size and type to the wheel element:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="wheel">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="brand" type="xsd:string"/>
   <xsd:element name="size" type="xsd:decimal"/>
   <xsd:element name="type" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>
</xsd:schema>

Defining the number of times an element may occur

By default an element defined inside of the sequence tags can only show up once in the XML data. With DTDs we changed this by appending +, ? or * character to the child. With schemas we use the minOccurs and maxOccurs attributes. As you may have guessed from their names minOccurs defines the minimum number of times an element must appear, and maxOccurs defines the maximum number of times an element may appear. The default value for both is 1, so if you don't set anything the element can appear exactly one time. If you want an element to appear more than once, but you don't know exactly how many times it may appear, set maxOccurs="unbounded". The following example creates a declaration for the student element, which can must occur at least once (minOccurs isn't set so it defaults to 1), but can occur an unlimited number of times.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="students">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="student" type="xsd:string" maxOccurs="unbounded" />
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>
</xsd:schema>

Defining the child element order

If you use the sequence element then the child elements must appear in the data in same order as their element definitions. You can also use the all element or choice element instead of sequence. All of these can be modified using minOccurs or maxOccurs.

all The elements can appear in any order
choice Only one of the elements can appear
sequence The elements must appear in the specified order

Here's a simple example that allows a car element to have either a licenseNumber child element or a registrationNumber child element.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="car">
 <xsd:complexType>
  <xsd:choice>
   <xsd:element name="licenseNumber" type="xsd:string"  />
   <xsd:element name="registrationNumber" type="xsd:string"  />
  </xsd:choice>
 </xsd:complexType>
</xsd:element>
</xsd:schema>

Remember that it's better to have a tight set of rules, so in most cases you don't want to use all. There may be times when you want to require some child elements but have a choice of some others. This can be done by nesting the order elements inside one another. Using the car from the previous example, if we wanted to require the car element to contain a make element and a model element, but a choice of either the licenseNumber or registrationNumber we would use the following:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="car">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="make" type="xsd:string"  />
   <xsd:element name="model" type="xsd:string"  />

  <xsd:choice>
   <xsd:element name="licenseNumber" type="xsd:string"  />
   <xsd:element name="registrationNumber" type="xsd:string"  />
  </xsd:choice>

  </xsd:sequence>
 </xsd:complexType>
</xsd:element>
</xsd:schema>

If we wanted to require the car element to have a choice of either the licenseNumber and state elements, or the the registrationNumber and registrationLocation elements we would use the following:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="car">
 <xsd:complexType>

  <xsd:choice>

  <xsd:sequence>
   <xsd:element name="licenseNumber" type="xsd:string"  />
   <xsd:element name="state" type="xsd:string"  />
  </xsd:sequence>

  <xsd:sequence>
   <xsd:element name="registrationNumber" type="xsd:string"  />
   <xsd:element name="registrationLocation" type="xsd:string"  />
  </xsd:sequence>

  </xsd:choice>

 </xsd:complexType>
</xsd:element>
</xsd:schema>