Complex Model Groups

Contact Us or call 1-877-932-8228
Complex Model Groups

Complex Model Groups

Content models can be made up of simple-type elements. Additionally, complex-type elements can contain other complex-type elements.

Syntax

<xs:element name="ElementName">
	<xs:complexType>
		<xs:choice>
			<xs:element name="Child1" type="xs:string"/>
			<xs:element name="Child2">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="GC1" type="xs:string"/>
						<xs:element name="GC2" type="xs:string"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="Child3" type="xs:string"/>
		</xs:choice>
	</xs:complexType>
</xs:element>

Furthermore, model groups can be nested within each other. The following example illustrates this. Notice that the choice model group, which allows for either a Salary element or a Wage element is nested with in a sequence model group. Both of the subsequent instances are valid according to this schema.

Code Sample:

ComplexTypes/Demos/Employee.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Salary">
		<xs:restriction base="xs:decimal">
			<xs:minInclusive value="10000"/>
			<xs:maxInclusive value="90000"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Employee">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Name">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="FirstName"/>
							<xs:element name="LastName"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:choice>
					<xs:element name="Salary" type="Salary"/>
					<xs:element name="Wage" type="xs:decimal"/>
				</xs:choice>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

ComplexTypes/Demos/DaveSmith.xml
<?xml version="1.0"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Employee.xsd">
	<Name>
		<FirstName>Dave</FirstName>
		<LastName>Smith</LastName>
	</Name>
	<Salary>90000</Salary>
</Employee>

Code Sample:

ComplexTypes/Demos/JillSmith.xml
<?xml version="1.0"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Employee.xsd">
	<Name>
		<FirstName>Jill</FirstName>
		<LastName>Smith</LastName>
	</Name>
	<Wage>20.50</Wage>
</Employee>
Next