XSD Cheatsheet

Complete quick reference for XML Schema (XSD) with copy-paste examples. Bookmark this page for easy access.

Basic XSD Structure

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.example.com"
           xmlns="http://www.example.com"
           elementFormDefault="qualified">

  <!-- Element declarations -->
  <xs:element name="root" type="RootType"/>

  <!-- Type definitions -->
  <xs:complexType name="RootType">
    <xs:sequence>
      <xs:element name="child" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

The root element xs:schema contains all declarations. Use targetNamespace to define the namespace for your schema.

Simple Type Elements

Element with Built-in Type

<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="active" type="xs:boolean"/>
<xs:element name="date" type="xs:date"/>

Element with Default/Fixed Value

<!-- Default value (can be overridden) -->
<xs:element name="country" type="xs:string" 
            default="USA"/>

<!-- Fixed value (cannot be changed) -->
<xs:element name="version" type="xs:string" 
            fixed="1.0"/>

Complex Types

Sequence (ordered elements)

<xs:complexType name="PersonType">
  <xs:sequence>
    <xs:element name="firstName" type="xs:string"/>
    <xs:element name="lastName" type="xs:string"/>
    <xs:element name="email" type="xs:string" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

Elements must appear in the specified order. Use minOccurs="0" for optional elements.

Choice (one of many)

<xs:complexType name="ContactType">
  <xs:choice>
    <xs:element name="email" type="xs:string"/>
    <xs:element name="phone" type="xs:string"/>
    <xs:element name="fax" type="xs:string"/>
  </xs:choice>
</xs:complexType>

Only one of the elements can appear. Use for mutually exclusive options.

All (unordered elements)

<xs:complexType name="AddressType">
  <xs:all>
    <xs:element name="street" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="zip" type="xs:string"/>
  </xs:all>
</xs:complexType>

Elements can appear in any order. Each element can appear at most once.

Mixed Content (text + elements)

<xs:complexType name="ParagraphType" mixed="true">
  <xs:sequence>
    <xs:element name="bold" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    <xs:element name="italic" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>

Allows text content mixed with child elements, like HTML paragraphs.

Simple Content with Attributes

<xs:complexType name="PriceType">
  <xs:simpleContent>
    <xs:extension base="xs:decimal">
      <xs:attribute name="currency" type="xs:string" use="required"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

<!-- Allows: <price currency="USD">19.99</price> -->

Type Restrictions (Facets)

String Length

<xs:simpleType name="UsernameType">
  <xs:restriction base="xs:string">
    <xs:minLength value="3"/>
    <xs:maxLength value="20"/>
  </xs:restriction>
</xs:simpleType>

Numeric Range

<xs:simpleType name="AgeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="150"/>
  </xs:restriction>
</xs:simpleType>

Pattern (Regex)

<xs:simpleType name="EmailType">
  <xs:restriction base="xs:string">
    <xs:pattern value="[^@]+@[^@]+\.[^@]+"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="PhoneType">
  <xs:restriction base="xs:string">
    <xs:pattern value="\d{3}-\d{3}-\d{4}"/>
  </xs:restriction>
</xs:simpleType>

Enumeration

<xs:simpleType name="StatusType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="pending"/>
    <xs:enumeration value="approved"/>
    <xs:enumeration value="rejected"/>
  </xs:restriction>
</xs:simpleType>

Whitespace Handling

<xs:simpleType name="NormalizedString">
  <xs:restriction base="xs:string">
    <!-- preserve, replace, or collapse -->
    <xs:whiteSpace value="collapse"/>
  </xs:restriction>
</xs:simpleType>

Decimal Precision

<xs:simpleType name="CurrencyType">
  <xs:restriction base="xs:decimal">
    <xs:totalDigits value="10"/>
    <xs:fractionDigits value="2"/>
  </xs:restriction>
</xs:simpleType>

All Restriction Facets

FacetApplies ToDescription
lengthstring, binaryExact length required
minLengthstring, binaryMinimum length
maxLengthstring, binaryMaximum length
patternstringRegex pattern to match
enumerationallList of allowed values
whiteSpacestringpreserve, replace, collapse
minInclusivenumeric, dateMinimum value (inclusive)
maxInclusivenumeric, dateMaximum value (inclusive)
minExclusivenumeric, dateMinimum value (exclusive)
maxExclusivenumeric, dateMaximum value (exclusive)
totalDigitsdecimalTotal number of digits
fractionDigitsdecimalDigits after decimal point

Occurrence Indicators

minOccurs / maxOccurs

<!-- Required (default) -->
<xs:element name="id" type="xs:string"/>

<!-- Optional (0 or 1) -->
<xs:element name="nickname" type="xs:string" 
            minOccurs="0"/>

<!-- Required, multiple (1 to unlimited) -->
<xs:element name="item" type="ItemType" 
            maxOccurs="unbounded"/>

<!-- Optional, multiple (0 to unlimited) -->
<xs:element name="tag" type="xs:string" 
            minOccurs="0" maxOccurs="unbounded"/>

<!-- Exactly 3 -->
<xs:element name="coordinate" type="xs:decimal" 
            minOccurs="3" maxOccurs="3"/>

<!-- Between 1 and 5 -->
<xs:element name="option" type="xs:string" 
            minOccurs="1" maxOccurs="5"/>

Common Patterns

PatternminOccursmaxOccurs
Required (1)1 (default)1 (default)
Optional (0-1)01
One or more (1+)1unbounded
Zero or more (0+)0unbounded

Attributes

Basic Attributes

<xs:complexType name="BookType">
  <xs:sequence>
    <xs:element name="title" type="xs:string"/>
  </xs:sequence>
  
  <!-- Required attribute -->
  <xs:attribute name="isbn" type="xs:string" 
                use="required"/>
  
  <!-- Optional attribute (default) -->
  <xs:attribute name="edition" type="xs:integer"/>
  
  <!-- Attribute with default value -->
  <xs:attribute name="language" type="xs:string" 
                default="en"/>
  
  <!-- Attribute with fixed value -->
  <xs:attribute name="format" type="xs:string" 
                fixed="hardcover"/>
</xs:complexType>

Attribute Groups

<!-- Define reusable attribute group -->
<xs:attributeGroup name="CommonAttrs">
  <xs:attribute name="id" type="xs:ID"/>
  <xs:attribute name="class" type="xs:string"/>
  <xs:attribute name="style" type="xs:string"/>
</xs:attributeGroup>

<!-- Use in complex type -->
<xs:complexType name="DivType">
  <xs:sequence>
    <xs:any processContents="lax" 
            minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attributeGroup ref="CommonAttrs"/>
</xs:complexType>

Built-in XSD Data Types

String Types

  • xs:string
  • xs:normalizedString
  • xs:token
  • xs:Name
  • xs:NCName
  • xs:ID
  • xs:IDREF
  • xs:IDREFS
  • xs:language

Numeric Types

  • xs:integer
  • xs:positiveInteger
  • xs:negativeInteger
  • xs:nonNegativeInteger
  • xs:nonPositiveInteger
  • xs:long
  • xs:int
  • xs:short
  • xs:byte
  • xs:decimal
  • xs:float
  • xs:double

Date/Time Types

  • xs:date (2024-01-15)
  • xs:time (14:30:00)
  • xs:dateTime
  • xs:duration
  • xs:gYear (2024)
  • xs:gMonth (--01)
  • xs:gDay (---15)
  • xs:gYearMonth
  • xs:gMonthDay

Boolean & Binary

  • xs:boolean (true/false)
  • xs:base64Binary
  • xs:hexBinary

URI Types

  • xs:anyURI
  • xs:QName
  • xs:NOTATION

Special Types

  • xs:anyType
  • xs:anySimpleType

Namespaces

Declaring Namespaces

<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:tns="http://example.com/myschema"
  targetNamespace="http://example.com/myschema"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified">

  <!-- Elements will use the target namespace -->
  <xs:element name="root" type="tns:RootType"/>

</xs:schema>

Import External Schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ext="http://example.com/external">

  <!-- Import schema from different namespace -->
  <xs:import namespace="http://example.com/external" 
             schemaLocation="external.xsd"/>

  <!-- Include schema from same namespace -->
  <xs:include schemaLocation="common-types.xsd"/>

  <!-- Use imported type -->
  <xs:element name="data" type="ext:DataType"/>

</xs:schema>

Put Your Knowledge to Practice

Use our free tools to create, convert, and validate XSD schemas.