Welcome to our free XML Schema tutorial. This tutorial is based on Webucator's Introduction to XML Schema course.
Tutorial
Lesson Goals
While attributes themselves must be of simple type, only complex-type elements can contain attributes.
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.
<?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>
<?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>
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.
<?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>
<?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>
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.
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.
<?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>
<?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>
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.
<?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.
<?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.
<?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>
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.
<?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>
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.
<?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>
By default, attributes are optional, but they can be required by setting the use attribute of xs:attribute to required as shown below.
<?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>
In this exercise, you will modify the song schema, so that it can successfully validate Attributes/Exercises/TheGirlIsMine.xml.
<?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>