Simple vs Complex Types in XML Schema
In XML Schema Definition (XSD), data types govern the structure, constraints, and valid contents of elements and attributes within an XML document. The core distinction between simple types and complex types lies in whether an element can contain child elements or attributes. While simple types are strictly limited to text-only values with no attributes or nested tags, complex types can hold child elements, attributes, or mixed content. Understanding this distinction is essential for designing well-structured, valid XML architectures.
What is a Simple Type?
A simple type defines an element or attribute that contains only text (character data). Elements defined with a simple type cannot contain child elements, nor can they carry attributes.
XSD provides numerous built-in simple types, such as
xs:string, xs:integer,
xs:boolean, and xs:date. Developers can also
derive custom simple types using the <xs:simpleType>
tag to enforce restrictions, patterns, lists, or unions on existing data
types.
Example of a Simple Type
<!-- XSD Definition -->
<xs:simpleType name="PostalCodeType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{5}"/>
</xs:restriction>
</xs:simpleType>
<!-- XML Usage -->
<postalCode>90210</postalCode>What is a Complex Type?
A complex type defines an element that can contain child elements, attributes, or a combination of both. Complex types structure the hierarchical relationships and container nodes within an XML document.
Defined using the <xs:complexType> tag, complex
types support multiple content models: * Element-only
content: Contains only nested child elements. * Mixed
content: Contains both text and child elements. * Empty
content: Contains no text or child elements, typically holding
only attributes. * Simple content: Contains only text,
but is extended to include one or more attributes.
Example of a Complex Type
<!-- XSD Definition -->
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="postalCode" type="PostalCodeType"/>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="optional"/>
</xs:complexType>
<!-- XML Usage -->
<address type="billing">
<street>123 Main St</street>
<city>Springfield</city>
<postalCode>90210</postalCode>
</address>Key Differences
| Feature | Simple Type | Complex Type |
|---|---|---|
| Child Elements | Cannot contain child elements | Can contain child elements |
| Attributes | Cannot have attributes | Can have attributes |
| XSD Element | <xs:simpleType> |
<xs:complexType> |
| Application | Applies to elements and attributes | Applies only to elements |
| Primary Role | Validating values and formats | Defining structural hierarchy |
Summary of Usage
- Use Simple Types for leaf nodes in your XML tree where you need to validate specific data formats, values, ranges, or text lengths (e.g., email addresses, ages, dates).
- Use Complex Types for container elements, parent structures, or any element that requires metadata via attributes.