XML Schema Simple Types

Simple types in XML Schema define elements and attributes that hold a single value — no child elements, no attributes. A price, a name, a date, a postal code — all of these are simple types. XSD provides many built-in simple types, and you create your own custom ones using xs:simpleType with restrictions, lists, or unions.

Built-In vs User-Defined Simple Types

XSD ships with dozens of ready-made simple types like xs:string, xs:integer, and xs:date. When none of those fits exactly, you define your own using xs:simpleType.

Three Ways to Create Custom Simple Types

1. xs:restriction — Narrow a base type

The most common method. Take a built-in type and add constraints.

2. xs:list — A space-separated list of values

The element holds multiple values separated by spaces, all of the same type.

3. xs:union — Accept values from multiple types

The element accepts a value that is valid for any one of the listed types.

Method 1: xs:restriction

Covered in depth in the restrictions topic. Here is a quick example:

<xs:simpleType name="GradeType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="A+"/>
    <xs:enumeration value="A"/>
    <xs:enumeration value="B"/>
    <xs:enumeration value="C"/>
    <xs:enumeration value="D"/>
    <xs:enumeration value="F"/>
  </xs:restriction>
</xs:simpleType>
Usage
<xs:element name="grade" type="GradeType"/>
Valid XML
<grade>A+</grade>
Invalid XML
<grade>A++</grade>

Method 2: xs:list — Space-Separated Lists

An xs:list element holds multiple values of the same simple type, separated by whitespace.

Declaring a List Type

<xs:simpleType name="IntegerListType">
  <xs:list itemType="xs:integer"/>
</xs:simpleType>

<xs:element name="scores" type="IntegerListType"/>
Valid XML
<scores>85 92 78 91 88</scores>
Invalid XML — non-integers in the list
<scores>85 ninety 78</scores>

Using a Custom Type as the List Item

<xs:simpleType name="ColorType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="red"/>
    <xs:enumeration value="green"/>
    <xs:enumeration value="blue"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="ColorListType">
  <xs:list itemType="ColorType"/>
</xs:simpleType>

<xs:element name="palette" type="ColorListType"/>
Valid XML
<palette>red blue green</palette>
Invalid XML — "yellow" not in the ColorType enumeration
<palette>red yellow green</palette>

Constraining List Length

You can apply length, minLength, and maxLength to control how many items appear in the list.

<xs:simpleType name="ThreeColorPalette">
  <xs:restriction>
    <xs:simpleType>
      <xs:list itemType="ColorType"/>
    </xs:simpleType>
    <xs:length value="3"/>
  </xs:restriction>
</xs:simpleType>
Valid XML — exactly 3 colors
<palette>red green blue</palette>
Invalid XML — only 2 colors
<palette>red green</palette>

Method 3: xs:union — Multiple Acceptable Types

A union type accepts a value if it is valid for any one of the specified member types.

Example: Size Can Be a String or Integer

A clothing tag might show size as "XL" or as "42" (chest size). A union accepts both.

<xs:simpleType name="ClothingSizeType">
  <xs:union memberTypes="xs:integer xs:string"/>
</xs:simpleType>

<xs:element name="size" type="ClothingSizeType"/>
Valid XML
<size>XL</size>
<size>42</size>

Union with Custom Types

<xs:simpleType name="SpecialPriceType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="free"/>
    <xs:enumeration value="contact-us"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="PriceOrSpecialType">
  <xs:union memberTypes="xs:decimal SpecialPriceType"/>
</xs:simpleType>

<xs:element name="price" type="PriceOrSpecialType"/>
All valid
<price>299.99</price>
<price>free</price>
<price>contact-us</price>

Named vs Anonymous Simple Types

Named Simple Type — reusable

<xs:simpleType name="IndianPIN">
  <xs:restriction base="xs:string">
    <xs:pattern value="[1-9][0-9]{5}"/>
  </xs:restriction>
</xs:simpleType>

<xs:element name="billingPIN"  type="IndianPIN"/>
<xs:element name="deliveryPIN" type="IndianPIN"/>

Anonymous Simple Type — inline, used once

<xs:element name="pinCode">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[1-9][0-9]{5}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Simple Type Methods Comparison

MethodUse WhenExample
xs:restrictionLimit range, pattern, or enumeration of one typeAge between 0 and 120
xs:listElement holds multiple space-separated values of same typeList of exam scores
xs:unionElement can hold values from different typesSize as "XL" or 42

Key Points to Remember

  • Simple types hold a single value — no child elements, no attributes.
  • xs:restriction narrows a base type using facets like enumeration and pattern.
  • xs:list creates a type for space-separated multiple values, all of the same type.
  • xs:union accepts values valid for any one of the listed member types.
  • Named simple types are reusable across multiple element and attribute declarations.
  • Anonymous simple types are declared inline and used only once.

Leave a Comment

Your email address will not be published. Required fields are marked *