Latest web development tutorials

XML Schema complex type - mixed content

Mixed composite type may contain attributes, elements and text.


Complex type with mixed content

XML element, "letter", containing text, and other elements:

<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>

The following schema declares this "letter" element:

<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>

Note: To make the character data can appear between the "letter" of the sub-elements, mixed attribute must be set to "true".<Xs: sequence> element tag (name, orderid and shipdate) means that the definition must appear in sequence within the "letter" element.

We can also give a name for the complexType element, and let the "letter" element type attribute references the complexType name (by this method, several elements can refer to the same complex type):

<xs:element name="letter" type="lettertype"/>

<xs:complexType name="lettertype" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>