Skip to content

XSD Mixed Elements

How to define XML Schema (XSD) mixed elements with mixed=true to combine text, attributes, and child elements in a complex type, including declaration methods and ordering rules.

A mixed complex type element can contain attributes, elements, and text.

Complex Types with Mixed Elements

Consider this example:

<mail>  
	Dear Mr. <name>John Smith</name>.  
	Your order <orderid>1234</orderid>  
	will be delivered on <deliverydate>2001-07-13</deliverydate>.  
</mail>

We can declare such Complex Text using the following ways:

Use mixed=true

Define complexType with attribute mixed set to true. mixed attribute allow to have character data between elements.

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

Use ComplexType alone

Define an element of complexType: in this way we can reuse it with more elements.

<xs:element name="student" type="studenttype"/>

<xs:complexType name="studenttype" mixed="true">
	<xs:sequence>
		<xs:element name="name" type="xs:string"/>
		<xs:element name="orderid" type="xs:positiveInteger"/>
		<xs:element name="deliverydate"  type="xs:date"/>
	</xs:sequence>
</xs:complexType>
Navigation