Fixed Values

Contact Us or call 1-877-932-8228
Fixed Values

Fixed Values

Element values can be fixed, meaning that, if they appear in the instance document, they must contain a specified value. Fixed elements are often used for boolean switches.

Code Sample:

SimpleTypes/Demos/EmployeeFixed.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:fractionDigits value="2"/>
			<xs:totalDigits value="7"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="JobTitle">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Sales Manager"/>
			<xs:enumeration value="Salesperson"/>
			<xs:enumeration value="Receptionist"/>
			<xs:enumeration value="Developer"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Employee">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Salary" type="Salary"/>
				<xs:element name="Title" type="JobTitle"/>
				<xs:element name="Status" type="xs:string" fixed="current" 
					minOccurs="0"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

The MinOccurs attribute is used to make the Status element optional. However, if it is used, it must contain the value current or be left empty, in which case, the value current is implied. The file SimpleTypes/Demos/LauraSmith.xml in the Demos folder validates against this schema.

Next