This documentation is archived and is not being maintained.

Complex Types in XML Schemas

Visual Studio .NET 2003

Complex types are user defined data types that can include other elements or attributes. Complex types can contain elements defined as either simple or complex. Complex types can also include attributes and groups, whereas simple types can only contain facets.

Complex types are defined using the complexType element and typically contain combinations of element, attribute, and group declarations, as well as references to globally declared elements and groups. A complex type can be thought of as a mini-schema that defines the valid structure and data contained within a specific element.

Although complex types can be either named or unnamed, the following examples demonstrate named complex types. For more information about unnamed types, see Named vs. Unnamed Types and Groups.

Typical Use of a Complex Type

A common scenario where a complex type would be used is as part of a schema that validates a purchase order by requiring a shipping address and a billing address. You would define a complex type that represents an address and declare shipTo and billTo elements of that complex type.

The following example shows how to create a complex type named usAddress:

<xs:complexType name="usAddress">
      <xs:element name="name" type="xs:string" />
      <xs:element name="street" type="xs:string" />
      <xs:element name="city" type="xs:string" />
      <xs:element name="state" type="xs:string" />
      <xs:element name="zip" type="xs:string" />

An element declared with the usAddress type would look like this:

<xs:element name="ShipTo" type="usAddress" />

The following example shows how to declare multiple elements using your defined complex type:

<xs:element name="customerInfo">
        <xs:element name="Name" type="xs:string" />
        <xs:element name="ShipTo" type="usAddress" />
        <xs:element name="BillTo" type="usAddress" />

This is a visual representation of the preceding code in the XML Designer. Notice the globally declared complex type named usAddress at the bottom, and how the ShipTo and BillTo elements are defined with the usAddress type in the customerInfo element at the top.


See Also

Walkthrough: Creating an XML Schema with the XML Designer | World Wide Web Consortium ( | XML Schemas and the XML Designer | XML Elements, Attributes, and Types | Named vs. Unnamed Types and Groups | User-Defined Simple Types in XML Schemas | Creating XML Schemas and Datasets | Relational Data in XML Schemas | XML Files