This is a cheat sheet for the XML Schema language. It is a follow up to the XML Cheat Sheet and summarizes this XML Schema Tutorial. There is also a DTD Cheat Sheet.
For reference this is the XML Schema homepage including specifications and a primer.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://a.com" xmlns="http://a.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://a.com note.xsd">
<xs:element name="start_date" type="xs:date"/>
<xs:attribute name="start_date" type="xs:date"/>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])+"/>
</xs:restriction>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
| Restriction | Description |
|---|---|
| enumeration | Defines a list of acceptable values |
| fractionDigits | Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero |
| length | Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero |
| maxExclusive | Specifies the upper bounds for numeric values (the value must be less than this value) |
| maxInclusive | Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
| maxLength | Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
| minExclusive | Specifies the lower bounds for numeric values (the value must be greater than this value) |
| minInclusive | Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
| minLength | Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
| pattern | Defines the exact sequence of characters that are acceptable |
| totalDigits | Specifies the exact number of digits allowed. Must be greater than zero |
| whiteSpace | Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Complex types can also be inlined as with simple types.
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType>
<xs:complexType name="shoetype">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="lettertype" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:group>
A reference to it within a sequence would look like
<xs:group ref="persongroup"/>
* An Attribute group is similar for example.
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> </xs:attributeGroup>
A reference to the attribute group in a complex type would look like :
<xs:attributeGroup ref="personattrgroup"/>
<xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutionGroup="name"/>
Substitution can be blocked with
<xs:element name="name" type="xs:string" block="substitution"/>
| Type | Description |
|---|---|
| date | Defines a date value (“YYYY-MM-DD”) |
| dateTime | Defines a date and time value (“YYYY-MM-DDThh:mm:ss”) |
| duration | Defines a time interval ([-]PnYnMnDTnHnMnS) |
| gDay | Defines a part of a date - the day (DD) |
| gMonth | Defines a part of a date - the month (MM) |
| gMonthDay | Defines a part of a date - the month and day (MM-DD) |
| gYear | Defines a part of a date - the year (YYYY) |
| gYearMonth | Defines a part of a date - the year and month (YYYY-MM) |
| time | Defines a time value (“hh:mm:ss”) |
| Name | Description |
|---|---|
| byte | A signed 8-bit integer |
| decimal | A decimal value |
| int | A signed 32-bit integer |
| integer | An integer value |
| long | A signed 64-bit integer |
| negativeInteger | An integer containing only negative values ( .., -2, -1.) |
| nonNegativeInteger | An integer containing only non-negative values (0, 1, 2, ..) |
| nonPositiveInteger | An integer containing only non-positive values (.., -2, -1, 0) |
| positiveInteger | An integer containing only positive values (1, 2, ..) |
| short | A signed 16-bit integer |
| unsignedLong | An unsigned 64-bit integer |
| unsignedInt | An unsigned 32-bit integer |
| unsignedShort | An unsigned 16-bit integer |
| unsignedByte | An unsigned 8-bit integer |