XML Schema 'complexType' element

Definition and Usage

The 'complexType' element defines a complex type. The elements of a complex type are XML elements that contain other elements and/or attributes.

Element information

Occurrence Unrestricted within the schema; once within the element.
Parent element element, redefine, schema
Content annotation, simpleContent, complexContent, group, all, choice, sequence, attribute, attributeGroup, anyAttribute

Syntax

<complexType
id=ID 
name=NCName 
abstract=true|false 
mixed=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>
(annotation?,(simpleContent|complexContent|((group|all| 
(choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))
</complexType>

(? Symbol is declared in the complexType element, the element can appear zero or one time, * Symbol declares the element can appear zero or more times.)

Attributes

id

Optional. Specifies the unique ID of the element.

name

Optional. Specifies the name of the element.

abstract

Optional. Specifies whether the complex type can be used in the instance document. If the value is true, the element cannot directly use the complex type but must use a derived complex type from it. The default value is false.

mixed

Optional. Specifies whether character data is allowed to appear between the child elements of this complex type. The default value is false.

  • If the simpleContent element is a child element, the mixed attribute is not allowed.
  • If the complexContent element is a child element, the mixed attribute can be overridden by the mixed attribute of the complexContent element.

block

Optional. Prevents the complex type with the specified derived type from being used to replace the complex type. The value can contain #all or a list, which is a subset of extension or restriction:

  • extension - Prevents the derived complex type from being used to replace the complex type itself.
  • restriction - Prevents the derived complex type from being used to replace the complex type itself.
  • #all - Prevents all derived complex types from being used to replace this complex type.

final

Optional. Prevents derivation of the specified type from this complexType element. The value can contain #all or a list that is a subset of extension or restriction.

  • extension - Prevents derivation through extension.
  • restriction - Prevents derivation through restriction.
  • #all - Prevents all derivatives (extensions and restrictions).

any attributes

Optional. Specifies any other attributes with a non-schema namespace.

Instance

Example 1

The following example has a complex type element named "note":

<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>

Example 2

In the following example, there is a complex type "fullpersoninfo" that extends the inherited type using three additional elements (address, city, and country) and is derived from another complex type "personinfo":

<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
  <xs:complexContent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

In the above example, the "employee" element must sequentially contain the following elements: "firstname", "lastname", "address", "city", and "country".