Occurrence Constraints

Contact Us or call 1-877-932-8228
Occurrence Constraints

Occurrence Constraints

By default, elements that are declared locally must show up once and only once within their parent. This constraint can be changed using the minOccurs and maxOccurs attributes. The default value of each of these attributes is 1. The value of minOccurs can be any non-negative integer. The value of maxOccurs can be any positive integer or unbounded, meaning that the element can appear an infinite number of times.

The example below shows how minOccurs can be used to make an element optional and how maxOccurs can be used to allow an element to be repeated indefinitely.

Code Sample:

ComplexTypes/Demos/Employee2.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="MiddleName" minOccurs="0"/>
							<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:element name="Responsibilities">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Responsibility" type="xs:string" 
								maxOccurs="unbounded"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Note that minOccurs and maxOccurs can also be applied to model groups (e.g, xs:sequence) to control the number of times a model group can be repeated.

Next