XML Schema Complex Types
A simple type holds a single value — a number, a string, a date. A complex type is richer: it can contain child elements, carry attributes, or both. Any element that contains other elements or has attributes must be declared as a complex type in XML Schema.
Defining a Complex Type
<xs:complexType name="TypeName"> <!-- content model (xs:sequence, xs:all, or xs:choice) --> <!-- attributes --> </xs:complexType>
Content Models Inside Complex Types
1. xs:sequence — Children in Order
All child elements must appear in the declared order. This is the most common content model.
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="pinCode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Valid XML
<address> <street>12, MG Road</street> <city>Bangalore</city> <state>Karnataka</state> <pinCode>560001</pinCode> </address>
2. xs:all — Children in Any Order
All declared child elements must appear, but in any order. Each can appear at most once.
<xs:complexType name="PersonType">
<xs:all>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="phone" type="xs:string"/>
</xs:all>
</xs:complexType>
Valid XML — children in different orders
<person> <phone>9876543210</phone> <name>Sunita Verma</name> <email>sunita@mail.com</email> </person>
3. xs:choice — Only One Child Allowed
The element must contain exactly one of the listed child elements.
<xs:complexType name="PaymentType">
<xs:choice>
<xs:element name="creditCard" type="xs:string"/>
<xs:element name="upiID" type="xs:string"/>
<xs:element name="netBanking" type="xs:string"/>
</xs:choice>
</xs:complexType>
Valid XML
<payment><upiID>user@paytm</upiID></payment>
Invalid XML — two choices
<payment> <upiID>user@paytm</upiID> <creditCard>4111...</creditCard> </payment>
Complex Types with Attributes
Attributes are declared inside the complex type, after the content model.
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="category" type="xs:string" use="optional"/>
</xs:complexType>
Complex Type with Text Content and Attributes
Sometimes an element carries both a text value and attributes. Use xs:simpleContent with xs:extension for this.
<xs:complexType name="PriceType">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="xs:string" default="INR"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Valid XML
<price currency="USD">29.99</price> <price>1499</price> <!-- currency defaults to INR -->
Anonymous vs Named Complex Types
Named Complex Type
Defined separately and reused by multiple elements using the type attribute.
<xs:complexType name="ContactType">
<xs:sequence>
<xs:element name="phone" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="customerContact" type="ContactType"/>
<xs:element name="supplierContact" type="ContactType"/>
Anonymous Complex Type
Defined directly inside an element declaration. Used only once.
<xs:element name="invoice">
<xs:complexType>
<xs:sequence>
<xs:element name="invoiceNo" type="xs:string"/>
<xs:element name="amount" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Extending Complex Types
A complex type can extend another — like inheritance in programming. The child type gets all parent elements plus new ones.
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="EmployeeType">
<xs:complexContent>
<xs:extension base="PersonType">
<xs:sequence>
<xs:element name="employeeID" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
EmployeeType includes name, email (from PersonType) plus employeeID and department.
Valid XML for EmployeeType
<employee> <name>Rajesh Kumar</name> <email>rajesh@company.com</email> <employeeID>EMP-2024-101</employeeID> <department>Engineering</department> </employee>
Content Model Comparison
| Content Model | Order Required | Repetition | Best For |
|---|---|---|---|
| xs:sequence | Yes — exact order | Yes (with minOccurs/maxOccurs) | Structured data like addresses, orders |
| xs:all | No — any order | Once each | Configuration data with flexible fields |
| xs:choice | Only one allowed | No (only one child) | Alternative values like payment method |
Key Points to Remember
- Complex types define elements that contain child elements or attributes.
- xs:sequence requires children in a specific order.
- xs:all allows children in any order but each appears at most once.
- xs:choice allows only one of the listed children.
- Use xs:simpleContent when an element has both a text value and attributes.
- Named complex types are reusable; anonymous types are used only once.
- xs:extension lets one complex type inherit and extend another.
