Restricting Attribute Values

Contact Us or call 1-877-932-8228
Restricting Attribute Values

Restricting Attribute Values

Attribute values are restricted in the same way that the values of simple-type elements are restricted. Below are three examples.

This first example shows how to restrict an attribute value by defining its type locally. You may test Attributes/Demos/HuckFinn.xml against this schema.

Code Sample:

Attributes/Demos/Book.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Name" type="xs:string"/>
						</xs:sequence>
						<xs:attribute name="Title">
							<xs:simpleType>
								<xs:restriction base="xs:string">
									<xs:enumeration value="Mr."/>
									<xs:enumeration value="Ms."/>
									<xs:enumeration value="Dr."/>
								</xs:restriction>
							</xs:simpleType>
						</xs:attribute>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

This second example shows how to restrict an attribute value by applying a globally defined simple type. You may test Attributes/Demos/TomSawyer.xml against this schema.

Code Sample:

Attributes/Demos/Book2.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="PersonTitle">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Mr."/>
			<xs:enumeration value="Ms."/>
			<xs:enumeration value="Dr."/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Name" type="xs:string"/>
						</xs:sequence>
						<xs:attribute name="Title" type="PersonTitle"/>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

This third example shows how to declare an attribute with a derived type globally. You may test Attributes/Demos/LifeOnTheMississippi.xml against this schema.

Code Sample:

Attributes/Demos/Book3.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:attribute name="Title">
		<xs:simpleType>
			<xs:restriction base="xs:string">
				<xs:enumeration value="Mr."/>
				<xs:enumeration value="Ms."/>
				<xs:enumeration value="Dr."/>
			</xs:restriction>
		</xs:simpleType>
	</xs:attribute>
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Name" type="xs:string"/>
						</xs:sequence>
						<xs:attribute ref="Title"/>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next