XSD - Complex Types
Complex Element is an XML element which can contain other elements and/or attributes. We can create a complex element in two ways −
Define a complex type and then create an element using the type attribute
Define a complex type directly by naming
Define a Complex Type and then create an element using type attribute.
<xs:complexType name = "StudentType">
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "lastname" type = "xs:string"/>
<xs:element name = "nickname" type = "xs:string"/>
<xs:element name = "marks" type = "xs:positiveInteger"/>
</xs:sequence>
<xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>
<xs:element name = 'student' type = 'StudentType' />
Define a Complex Type directly by naming.
<xs:element name = "student">
<xs:complexType>
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "lastname" type = "xs:string"/>
<xs:element name = "nickname" type = "xs:string"/>
<xs:element name = "marks" type = "xs:positiveInteger"/>
</xs:sequence>
<xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>
<xs:element>
Following is the list of Complex Types that XSD supports.
| S.No. | Simple Type & Description |
|---|---|
| 1 |
Complex Empty complex type element can only have attributes but no contents. |
| 2 |
Elements-Only complex type element can only contain elements |
| 3 |
Text-Only complex type element can only contain attribute and text. |
| 4 |
Mixed complex type element can contain element, attribute and text. |
| 5 |
Indicators controls the ways how elements are to be organized in an XML document. |
| 6 |
The <any> element is used for elements which are not defined by schema |
| 7 |
The <anyAttribute> attribute is used for attribute which are not defined by schema. |