Default and Fixed Values

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

Default and Fixed Values

Default Values

Attributes can have default values. To specify a default value, use the default attribute of the xs:attribute element. Default values for attributes work slightly differently than they do for elements. If the attribute is not included in the instance document, the schema processor inserts it with the default value. You may test Attributes/Demos/NatHawthorne2.xml against this schema.

Code Sample:

Attributes/Demos/Author4.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
---- C O D E   O M I T T E D ----

							<xs:element name="FirstName">
								<xs:complexType>
									<xs:simpleContent>
										<xs:extension base="xs:string">
											<xs:attribute name="Full" type="xs:boolean" default="true"/>
										</xs:extension>
									</xs:simpleContent>
								</xs:complexType>
							</xs:element>
---- C O D E   O M I T T E D ----

</xs:schema>

Fixed Values

Attribute values can be fixed, meaning that, if they appear in the instance document, they must contain a specified value. Like with simple-type elements, this is done with the fixed attribute. You may test Attributes/Demos/NatHawthorne3.xml against this schema.

Code Sample:

Attributes/Demos/Author5.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
---- C O D E   O M I T T E D ----

				<xs:element name="Name">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="FirstName">
								<xs:complexType>
									<xs:simpleContent>
										<xs:extension base="xs:string">
											<xs:attribute name="Full" type="xs:boolean" default="true"/>
										</xs:extension>
									</xs:simpleContent>
								</xs:complexType>
							</xs:element>
							<xs:element name="LastName" type="xs:string"/>
						</xs:sequence>
						<xs:attribute name="Pseudonym" type="xs:boolean" fixed="true"/>
						<xs:attribute name="HomePage" type="xs:anyURI"/>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
---- C O D E   O M I T T E D ----

</xs:schema>
Next