facebook google plus twitter
Webucator's Free XML Schema Tutorial

Lesson: Attributes

Welcome to our free XML Schema tutorial. This tutorial is based on Webucator's Introduction to XML Schema course.

Tutorial

Lesson Goals

  • To define empty elements.
  • To declare element attributes.
  • To restrict attribute values.

Overview

While attributes themselves must be of simple type, only complex-type elements can contain attributes.

Empty Elements

An empty element is an element that contains no content, but it may have attributes. The HomePage element in the instance document below is an empty element. Below the instance is the snippet from the Author.xsd schema that declares the HomePage element.

Code Sample:

Attributes/Demos/MarkTwain.xml
<?xml version="1.0"?>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Author.xsd">
	<Name>
		<FirstName>Mark</FirstName>
		<LastName>Twain</LastName>
	</Name>
	<HomePage URL="http://www.marktwain.com"/>
</Author>

Code Sample:

Attributes/Demos/Author.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="HomePage">
					<xs:complexType>
						<xs:attribute name="URL" type="xs:anyURI"/>
					</xs:complexType>
				</xs:element>
---- C O D E   O M I T T E D ----

</xs:schema>

Adding Attributes to Elements with Complex Content

Elements that have child elements are said to contain complex content. Attributes for such elements are declared after the element's model group. For example, the Name element in the XML instance below has two child elements and two attributes. Below the instance is the snippet from the Author2.xsd schema that declares the Name element.

Code Sample:

Attributes/Demos/MarkTwain2.xml
<?xml version="1.0"?>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Author2.xsd">
	<Name Pseudonym="true" HomePage="http://www.marktwain.com">
		<FirstName>Mark</FirstName>
		<LastName>Twain</LastName>
	</Name>
</Author>

Code Sample:

Attributes/Demos/Author2.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" type="xs:string"/>
							<xs:element name="LastName" type="xs:string"/>
						</xs:sequence>
						<xs:attribute name="Pseudonym" type="xs:boolean"/>
						<xs:attribute name="HomePage" type="xs:anyURI"/>
					</xs:complexType>
				</xs:element>
---- C O D E   O M I T T E D ----

</xs:schema>

Adding Attributes to Elements with Simple Content

An element with simple content is one that only contains character data. If such an element contains one or more attributes, then it is a complex-type element. Elements with simple content and attributes are declared using the xs:simpleContent element and then extending the element with the xs:extension element, which must specify the type of simple content contained with the base attribute. The syntax is shown below.

<xs:element name="ElementName"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="AttName" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>

For example, the FirstName element in the XML instance below contains only simple content and has a single attribute. Below the instance is the snippet from the Author3.xsd schema that declares the FirstName element.

Code Sample:

Attributes/Demos/NatHawthorne.xml
<?xml version="1.0"?>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Author3.xsd">
	<Name Pseudonym="true" HomePage="http://www.nathanielhawthorne.com">
		<FirstName Full="false">Nat</FirstName>
		<LastName>Hawthorne</LastName>
	</Name>
</Author>

Code Sample:

Attributes/Demos/Author3.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"/>
										</xs:extension>
									</xs:simpleContent>
								</xs:complexType>
							</xs:element>
---- C O D E   O M I T T E D ----

</xs:schema>

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>

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>

Requiring Attributes

By default, attributes are optional, but they can be required by setting the use attribute of xs:attribute to required as shown below.

Code Sample:

Attributes/Demos/Author6.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" use="required"/>
					</xs:complexType>
				</xs:element>
---- C O D E   O M I T T E D ----

</xs:schema>

Adding Attributes to Elements

Duration: 15 to 20 minutes.

In this exercise, you will modify the song schema, so that it can successfully validate Attributes/Exercises/TheGirlIsMine.xml.

  1. Open Attributes/Exercises/Song.xsd for editing.
  2. Change the schema so that the Title element can take the Type attribute, which is of type xs:string.
  3. Change the schema so that the Stanza element can take an Artist attribute, which is of type xs:string. The attribute should be required.
  4. Try to validate Attributes/Exercises/TheGirlIsMine.xml against the schema you just created. If the XML document is invalid, fix your schema.

Solution:

Attributes/Solutions/Song.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Song">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title">
					<xs:complexType>
						<xs:simpleContent>
							<xs:extension base="xs:string">
								<xs:attribute name="Type" type="xs:string"/>
							</xs:extension>
						</xs:simpleContent>
					</xs:complexType>
				</xs:element>
				<xs:element name="Year" type="xs:gYear"/>
				<xs:element name="Length" type="xs:string"/>
				<xs:element name="Artists">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Artist" type="xs:string" maxOccurs="unbounded"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element name="Lyrics">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Stanza" maxOccurs="unbounded">
								<xs:complexType>
									<xs:sequence>
										<xs:element name="Line" type="xs:string" maxOccurs="unbounded"/>
									</xs:sequence>
									<xs:attribute name="Artist" use="required" type="xs:string"/>
								</xs:complexType>
							</xs:element>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>