# XML Schema

Welcome to the XML Schema book. It describes the structure of an XML Schema and explains how XML Schemas are used to validate XML documents.

## Prerequisites

Students reading this book should already be familiar with the fundamental principles of XML and have some background on Data Types.

## Course Guidelines

Only the The prefix, The root element: <xs:schema> and Elements sections are required to build XML Schemas. History of the XML Schema, What are XML Schemas used For?, When XML Schema become inefficient at validating complex rules and XML Schema Example can be skipped and the sections following the Elements section are additional information starting from the most important ones.

## History of the XML Schema

XML Schema is a standard created by the world wide web consortium http://www.w3c.org. Unlike DTDs, XML Schema uses XML file formats to define the XML Schema itself. Once you learn the structure of an XML file, you don't have to learn another syntax.

## What are XML Schemas used For?

XML Schemas are primarily used to validate the structure and data types of an XML document. By structure we mean that an XML Schema defines:
* what data elements are expected
* what order the data elements are expected in
* what nesting these data elements have
* what data elements are optional and what data elements are required
XML Schemas can also be used by XML data mapping tools for quickly extracting data from databases and transferring them in XML files.
One of the best analogies is the blueprint analogy. Just like there are architectural blueprints that describe the structural design of a house, an XML Schema provides the "structural design" of a file.

## When XML Schema become inefficient at validating complex rules

Although XML Schemas are excellent at sequential validation of data elements and data types, XML Schema tend to be cumbersome at expressing highly complex business rules. For example when you are at the end of a large file it is difficult to state a rule that checks if a data element has some value that another data element at the beginning of the file should have had another values. This can be done by using XML transforms and XPath expressions.

## XML Schema Example

Here is a full example of a complete XML Schema file of personal contacts.
| XML Schema file | A compliant XML instance file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> <xs:element name="Contacts"> <xs:annotation> <xs:documentation> File of personal contacts. </xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence maxOccurs="unbounded"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> <ex:Contacts xmlns:ex="http://www.example.org/contactExample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd "> <ex:Person> <ex:PersonGivenName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> </ex:Person> </ex:Contacts> |

## Structure of an XML Schema Document

Like any other XML file, XML Schema files normally begin with an XML declaration (<?xml version="1.0"?>), which is followed by a root element (always xs:schema).

### The prefix

Although any prefix can be used to refer to the namespace http://www.w3.org/2001/XMLSchema, the most common convention is to use "xs". Some people prefer "xsd"; some prefer to use the default namespace (which means no prefix is necessary). All XML Schema elements are in this namespace.

### The root element: <xs:schema>

All XML Schema files must begin and end with the <xs:schema> markup.
| XML Schema file | A compliant XML file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> ...other structure here... </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> ...any elements here... |
The schema MUST end with an </xs:schema> end markup.
An XML Schema defines elements and attributes which are available in a namespace (i.e. http://www.example.org/contactExample). In the XML Schema, this namespace is defined using the targetNamespace attribute.
In an XML file, a namespace can be imported using the xmlns attribute (xmlns stands for XML NameSpace). The xmlns attribute name can be ended with : and a prefix (i.e. xs). In this case, the imported tags must be used with this prefix. Prefix are used to distinguish tags with same names imported from different namespaces.
You can see that the target namespace we are defining in the example is one of the namespace imported in the XML file. You can see that we are importing the namespace of the document itself with the tns prefix, so that elements we are defining in the document can be used in the document itself starting with tns: .
<xs:sequence> is used for an ordered group of elements. For an unordered group of elements use <xs:all>.

### Elements

Elements are defined in XML Schema using the <xs:element> markup:
| XML Schema file | A compliant XML file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> <xs:element name="Contacts"/> </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> <ex:Contacts xmlns:ex="http://www.example.org/contactExample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd "/> |

### Elements with text body

For elements with text body, the type of the text can be defined with the type attribute:
| XML Schema file | A compliant XML file example |
| ... <xs:element name="PersonGivenName" type="xs:string"/> ... | ... <ex:PersonGivenName>Sue</ex:PersonGivenName> ... |
| ... <xs:element name="PersonFamilyName" type="xs:string"/> ... | ... <ex:PersonFamilyName>Smith</ex:PersonFamilyName> ... |
| ... <xs:element name="PersonBirthDate" type="xs:date"/> ... | ... <ex:PersonBirthDate>1990-09-23</ex:PersonBirthDate> ... |
Here are the common XML Schema primitive data types:
| Type | Usage | Legal value examples |
| xs:anyURI | <xs:element name = “url” type = “xs:anyURI” /> | http://www.w3.com |
| xs:boolean | <xs:element name = “hasChildren” type = “xs:boolean” /> | true or false or 1 or 0 |
| xs:byte | <xs:element name = “stdDev” type = “xs:byte” /> | -128 through 127 |
| xs:date | <xs:element name = “dateEst” type = “xs:date” /> | 2004-03-15 |
| xs:dateTime | <xs:element name = “xMas” type = “xs:dateTime” /> | 2003-12-25T08:30:00 |
| xs:decimal | <xs:element name = “pi” type = “xs:decimal” /> | 3.1415292 |
| xs:double | <xs:element name = “pi” type = “xs:double” /> | 3.1415292 or INF or NaN |
| xs:duration | <xs:element name = “MITDuration” type = “xs:duration” /> | P8M3DT7H33M2S |
| xs:float | <xs:element name = “pi” type = “xs:float” /> | 3.1415292 or INF or NaN |
| xs:gDay | <xs:element name = “dayOfMonth” type = “xs:gDay” /> | ---11 |
| xs:gMonth | <xs:element name = “monthOfYear” type = “xs:gMonth” /> | --02 (the format --02-- is often listed, but is wrong) |
| xs:gMonthDay | <xs:element name = “valentine” type = “xs:gMonthDay” /> | --02-14 |
| xs:gYear | <xs:element name = “year” type = “xs:gYear” /> | 1999 |
| xs:gYearMonth | <xs:element name = “birthday” type = “xs:gYearMonth” /> | 1972-08 |
| xs:ID | <xs:attribute name="id" type="xs:ID"/> | id-102 |
| xs:IDREF | <xs:attribute name="version" type="xs:IDREF"/> | id-102 |
| xs:IDREFS | <xs:attribute name="versionList" type="xs:IDREFS"/> | id-102 id-103 id-100 |
| xs:int | <xs:element name = “age” type = “xs:int” /> | 77 |
| xs:integer | <xs:element name = “age” type = “xs:integer” /> | 77 |
| xs:long | <xs:element name = “cannelNumber” type = “xs:long” /> | 214 |
| xs:negativeInteger | <xs:element name = “belowZero” type = “xs:negativeInteger” /> | -123 |
| xs:nonNegativeInteger | <xs:element name = “numOfchildren” type = “xs:nonNegativeInteger” /> | 2 |
| xs:nonPositiveInteger | <xs:element name = “debit” type = “xs:nonPositiveInteger” /> | 0 |
| xs:positiveInteger | <xs:element name = “credit” type = “xs:positiveInteger” /> | 500 |
| xs:short | <xs:element name = “numOfpages” type = “xs:short” /> | 476 |
| xs:string | <xs:element name = “name” type = “xs:string” /> | Joseph |
| xs:time | <xs:element name = “credit” type = “xs:time” /> | 13:02:00 |
Some schema restrictions and facets can be defined to the data type using the <xs:simpleType/> and the <xs:restriction/> markups. For instance, a body text of string type can be fixed to a length of 5 characters using the <xs:length/> markup as above:
| XML Schema file | A compliant XML file example |
| ... <xs:element name="PersonGivenName"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="5"/> </xs:restriction> </xs:simpleType> </xs:element> ... | ... <ex:PersonGivenName>Kevin</ex:PersonGivenName> ... |
| ... <xs:element name="PersonGivenName"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="5"/> </xs:restriction> </xs:simpleType> </xs:element> ... | ... <ex:PersonGivenName>Helen</ex:PersonGivenName> ... |
Here are all the schema restrictions and facets that can be used:
| Restriction | Explanation | Allowed restriction value |
| minExclusive | Specify the value that is just lesser than all the allowed values for any ordered data type | Any integer, decimal, date or time |
| minInclusive | Specify the minimum allowed value for any ordered data type | Any integer, decimal, date or time |
| maxExclusive | Specify the value that is just greater than all the allowed values for any ordered data type | Any integer, decimal, date or time |
| maxInclusive | Specify the greatest allowed value for any ordered data type | Any integer, decimal, date or time |
| totalDigits | Specify the maximum number of digits allowed to the left and the right of the decimal point | A positive integer |
| fractionDigits | Specify the maximum number of digits allowed to the right of the decimal point | A non negative integer |
| length | Specify the exact number of characters allowed | A non negative integer |
| minLength | Specify the minimum number of characters allowed | A non negative integer |
| maxLength | Specify the maximum number of characters allowed | A non negative integer |
| enumeration | Specify an allowed value or a set of allowed values | Any |
| whiteSpace | If "preserve" value is specified, any character is kept as it is. If "replace" value is specified, any tab (#x9), line feed (#xA) and carriage return (#xD) are replaced with space (#x20). If "collapse" value is specified, any tab, line feed and carriage return is replaced with space too, consecutive spaces are merged into a single space and beginning and ending spaces are deleted. | "preserve", "replace" or "collapse" |
| pattern | Specify a regular expression the value must match | A Regular Expression |

### Complex element

Any element type that can have sub-elements and/or attributes is considered as a complex element. Complex element types are defined using the <xs:complexType/> markup.

#### Sub-elements of a complex element

Sub-elements are defined with different apparition rules.
Sub-elements can be defined into a <xs:all/> markup: the sub-elements must all exist and can appear in any order.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:all> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:all> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonBirthDate/> <ex:PersonFamilyName/> <ex:PersonGivenName/> </ex:Person> ... |
Sub-elements can be defined into a <xs:sequence/> markup: the sub-elements must appear in the same order.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:sequence> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonGivenName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> </ex:Person> ... |
Sub-elements can be defined into a <xs:choice/> markup: one and only one sub-element must appear.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:choice> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:choice> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonGivenName/> </ex:Person> ... |
| ... <xs:element name="Person"> <xs:complexType> <xs:choice> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:choice> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonFamilyName/> </ex:Person> ... |
| ... <xs:element name="Person"> <xs:complexType> <xs:choice> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:choice> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonBirthDate/> </ex:Person> ... |
Some apparition rules can be included into others apparition rules. <xs:choice/> markup and <xs:sequence/> markup can be included into a <xs:choice/> markup or a <xs:sequence/> markup.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:choice> <xs:element name="PersonBirthDate" /> <xs:element name="PersonAge" /> </xs:choice> </xs:sequence> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonGivenName /> <ex:PersonFamilyName /> <ex:PersonAge /> </ex:Person> ... |
| ... <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:choice> <xs:element name="PersonBirthDate" /> <xs:element name="PersonAge" /> </xs:choice> </xs:sequence> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonGivenName /> <ex:PersonFamilyName /> <ex:PersonBirthDate /> </ex:Person> ... |
Number of occurrences can be changed with the minOccurs and maxOccurs attributes of the <xs:element/> markup.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" minOccurs="0" maxOccurs="1"/> <xs:element name="PersonFamilyName" minOccurs="3" maxOccurs="unbounded"/> <xs:element name="PersonBirthDate" minOccurs="2" maxOccurs="2"/> </xs:sequence> </xs:complexType> </xs:element> ... | ... <ex:Person> <ex:PersonFamilyName/> <ex:PersonFamilyName/> <ex:PersonFamilyName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> <ex:PersonBirthDate/> </ex:Person> ... |
By default, the minimum occurrence is 1 and the maximum occurrence is 1. To define an infinite number of occurrence, specify unbounded.

##### Complex element with sub-elements and text

Complex elements can contain text in their body before, between and after their sub-elements setting the mixed attribute to true.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> </xs:sequence> </xs:complexType> </xs:element> ... | ... <ex:Person> You can add text before, <ex:PersonGivenName /> between, <ex:PersonFamilyName /> and after the sub-elements. </ex:Person> ... |
By default, the mixed attribute is set to false.

#### Attributes of a complex element

The attributes of an element can be defined with the <xs:attribute/> markup.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:attribute name="relation"/> </xs:complexType> </xs:element> ... | ... <ex:Person relation="a colleague"/> ... |
If the element contains both attributes and sub-elements, the <xs:attribute/> markups must be defined above the <xs:all/>, <xs:sequence/> or <xs:choice/> markup. Data types, restrictions and facets can be defined for attributes as it is for text-body-only elements.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> </xs:sequence> <xs:attribute name="professional" type=“xs:boolean”/> <xs:attribute name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="woman"/> <xs:enumeration value="man"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> ... | ... <ex:Person professional="true" gender="woman"> <ex:PersonGivenName /> </ex:Person> ... |
By default, attributes are optional. This can be changed with the use attribute.
* If its value is optional, the attribute can be left.
* If its value is required, the attribute must be present.
A default value can be defined with the default attribute. If the attribute is not present, the parsers will consider the attribute is present and its value is the default value.
The attribute can be restricted to a constant value with the fixed attribute. As the fixed attribute also acts as a default value, you must not define a default attribute too.
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:attribute name="professional" use=“required”/> <xs:attribute name="gender" fixed="woman"/> <xs:attribute name="comment" default="no comment"/> </xs:complexType> </xs:element> ... | ... <ex:Person professional="true" comment="supplier"/> ... |
| ... <xs:element name="Person"> <xs:complexType> <xs:attribute name="professional" use=“required”/> <xs:attribute name="gender" fixed="woman"/> <xs:attribute name="comment" default="no comment"/> </xs:complexType> </xs:element> ... | ... <ex:Person gender="woman" professional="false"/> ... |

##### Complex element with attributes and text body

Elements can contain both attributes and text body using the <xs:simpleContent/> markup (remember that a simple type element can't contain attributes).
| XML Schema file | A compliant XML file example |
| ... <xs:element name="Person"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:positiveInteger"> <xs:attribute name="lastUpdate" type="xs:date" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> ... | ... <ex:Person lastUpdate="2009-09-10"> 1864 </ex:Person> ... |

### Type definition

Simple and complex types can be defined beside the element tree. In this case, the <xs:element/> markup has no body, keeps its name attribute and has a type attribute. The <xs:complexType/> markup is then defined outside the root element with a name attribute containing the element type name. There is no change for the XML file validation. Let's take this XML Schema:
| XML Schema file | A compliant XML file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> <xs:element name="Contacts"> <xs:complexType> <xs:sequence> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="LastUpdate"> <xs:simpleType> <xs:restriction base="xs:date"> <xs:minExclusive value="2003-05-06"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> <ex:Contacts xmlns:ex="http://www.example.org/contactExample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd" LastUpdate="2003-05-07"> <ex:Person> <ex:PersonGivenName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> </ex:Person> </ex:Contacts> |
Now let's define Person complex type and the LastUpdate simple type beside the root element tree:
| XML Schema file | A compliant XML file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> <xs:complexType name="PersonType"> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:sequence> </xs:complexType> <xs:simpleType name="LastUpdateType"> <xs:restriction base="xs:date"> <xs:minExclusive value="2003-05-06"/> </xs:restriction> </xs:simpleType> <xs:element name="Contacts"> <xs:complexType> <xs:sequence> <xs:element name="Person" type="PersonType"/> </xs:sequence> <xs:attribute name="LastUpdate" type="LastUpdateType"/> </xs:complexType> </xs:element> </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> <ex:Contacts xmlns:ex="http://www.example.org/contactExample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd" LastUpdate="2003-05-07"> <ex:Person> <ex:PersonGivenName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> </ex:Person> </ex:Contacts> |
Complex and simple types can be defined in any order. A defined type can be reused in different elements of the schema and then its description is not duplicated. It avoids the XSD file to be too much indented. Moreever, using type definitions, the elements have not only a name but also a type name which can be used as a class name too. Some tools used to parse XML content according to an XML Schema can require a type name for complex type elements.

### Element and attribute reference

Elements and attributes can be reused using references. In this case, the <xs:element/> markup or <xs:attribute/> markup has no body, no name attribute and has a ref attribute. This ref attribute contains the name of another element or another attribute. Let's use a reference on the Person element on the previous example:
| XML Schema file | A compliant XML file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:sequence> </xs:complexType> </xs:element> <xs:simpleType name="LastUpdateType"> <xs:restriction base="xs:date"> <xs:minExclusive value="2003-05-06"/> </xs:restriction> </xs:simpleType> <xs:element name="Contacts"> <xs:complexType> <xs:sequence> <xs:element ref="Person"/> </xs:sequence> <xs:attribute name="LastUpdate" type="LastUpdateType"/> </xs:complexType> </xs:element> </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> <ex:Contacts xmlns:ex="http://www.example.org/contactExample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd" LastUpdate="2003-05-07"> <ex:Person> <ex:PersonGivenName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> </ex:Person> </ex:Contacts> |
The difference between separate type definition above and using reference is that any element or attribute can be referenced. Moreever, using reference, links are done using names instead of type names. This means the we are not linking classes but instances of classes.

### Extension

Defined complex types can be reused adding sub-elements or attributes. The complex type is then extended. It can be done using the <xs:complexContent/> and the <xs:extension/> markups. The extended type name is defined in the base attribute of the <xs:extension/> markup. Here is an example where the PersonType complex type is extended with the professional attribute for the Person element:
| XML Schema file | A compliant XML file example |
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/contactExample" xmlns:tns="http://www.example.org/contactExample" elementFormDefault="qualified"> <xs:complexType name="PersonType"> <xs:sequence> <xs:element name="PersonGivenName" /> <xs:element name="PersonFamilyName" /> <xs:element name="PersonBirthDate" /> </xs:sequence> </xs:complexType> <xs:element name="Contacts"> <xs:complexType> <xs:sequence> <xs:element name="Person"> <xs:complexType> <xs:complexContent> <xs:extension base="PersonType"> <xs:attribute name="professional" type="xs:boolean"/> </xs:extension> </xs:complexContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> | <?xml version="1.0" encoding="UTF-8"?> <ex:Contacts xmlns:ex="http://www.example.org/contactExample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd"> <ex:Person professional="true"> <ex:PersonGivenName/> <ex:PersonFamilyName/> <ex:PersonBirthDate/> </ex:Person> </ex:Contacts> |
Various elements with common and different sub-elements or attributes can be defined like that. The common items would be defined in a common complex type and the different items would be defined in different complex types extending the first one.

### Instance Document Attributes

| Attribute | Explanation | Example |
| xsi:nil | Indicates that a certain element does not have a value or that the value is unknown. The element must be set to nillable inside the schema document: <xs:element name=”last_name” type=”xs:string” nillable=true”/> | <full_name xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance”> <first_name>Madonna</first_name> <last_name xsi:nil=”true”/> </full_name> |
| xsi:noNamespaceSchemaLocation | Locates the schema for elements that are not in any namespace | <radio xsi:noNamespaceSchemaLocation= ”http://www.opentourism.org/xmtext/radio.xsd”> <!—radio stuff goes here -- > </radio> |
| xsi:schemaLocation | Locates schemas for elements and attributes that are in a specified namespace | <radio xmlns= ”http://www.opentourism.org/xmtext/NS/radio xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation= ”http://www.arches.uga.eduNS/radio” ”http://www.opentourism.org/xmtext/radio.xsd”> <!—radio stuff goes here -- > </radio> |
| xsi:type | Can be used in instance documents to indicate the type of an element. | <height xsi:type=”xs:decimal”>78.9</height> |

## See also

* World Wide Web Schema