11. XML Data Types in XSD

What you will learn

  • The built-in data types you use most
  • How to restrict a type with facets
  • How to make your own named simple type
  • How to allow only certain values, or a pattern

Why data types?

To a plain XML parser, <age>25</age> and <age>twenty</age> are both just text. A schema can say that age must be a whole number, so the second one is rejected before your program ever sees it.

Common built-in types

Type Holds Valid example
xs:string Any text Hello
xs:integer A whole number, positive or negative -42
xs:positiveInteger A whole number above zero 7
xs:nonNegativeInteger Zero or more 0
xs:decimal A number with a decimal part 12.50
xs:boolean True or false true, false, 1, 0
xs:date A calendar date: year, month, day 2026-03-15
xs:time A time of day 14:30:00
xs:dateTime A date and time together 2026-03-15T14:30:00
xs:anyURI A web address https://example.com/
xs:ID A unique name in the document b1

Dates follow the international year-month-day order, with two digits for the month and day.

Using types

XML code: order.xsd

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

  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="customer" type="xs:string"/>
        <xs:element name="quantity" type="xs:positiveInteger"/>
        <xs:element name="total" type="xs:decimal"/>
        <xs:element name="gift" type="xs:boolean"/>
        <xs:element name="date" type="xs:date"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

XML code: order.xml (valid)

<order>
  <customer>Sam</customer>
  <quantity>3</quantity>
  <total>37.50</total>
  <gift>true</gift>
  <date>2026-03-15</date>
</order>

What would be rejected

  • <quantity>0</quantity> or <quantity>two</quantity> (not a positive integer)
  • <gift>maybe</gift> (not a boolean)
  • <date>15/03/2026</date> (wrong date format)

Restrictions and facets

You can narrow a type with a restriction. Inside it, facets set the limits.

Facet Limits
minInclusive, maxInclusive A number range, ends included
minExclusive, maxExclusive A number range, ends excluded
length, minLength, maxLength The number of characters
enumeration A list of allowed values
pattern A regular expression the text must match
totalDigits, fractionDigits The digits in a decimal
whiteSpace How spaces are treated

A number in a range

XML code

<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="13"/>
      <xs:maxInclusive value="120"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

What it accepts

<age>25</age> is valid. <age>10</age> and <age>200</age> are rejected.

A list of allowed values

XML code

<xs:element name="level">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="beginner"/>
      <xs:enumeration value="intermediate"/>
      <xs:enumeration value="advanced"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

What it accepts

Only the three listed words. <level>expert</level> is rejected.

A length limit

XML code

<xs:element name="username">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:minLength value="4"/>
      <xs:maxLength value="20"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

A pattern

A pattern uses a regular expression. The value must match the whole pattern. This one accepts a code such as AB-1234: two capital letters, a hyphen, then four digits.

XML code

<xs:element name="code">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]{2}-[0-9]{4}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

What it accepts

AB-1234 is valid. ab-1234, AB-123, and AB-12345 are rejected.

Naming your own types

If you use the same restriction in several places, give it a name and reuse it.

XML code: named types

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

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

  <xs:element name="result">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="maths" type="percentType"/>
        <xs:element name="science" type="percentType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

Both maths and science must now be whole numbers from 0 to 100. Change the rule in one place, and it changes everywhere.

Lists and unions

  • An xs:list type accepts several values separated by spaces, such as 1 2 3.
  • An xs:union accepts a value that matches any one of several types.

They are less common, so look them up in the XML Schema reference when you need them.

A note on checking numbers

Types only describe format. A schema cannot check rules that depend on other values, such as “the end date must be after the start date”. Such rules need extra code, or newer schema features. Always keep your own validation in your program too.

Common mistakes

  • Using xs:integer and expecting to forbid zero. Use xs:positiveInteger.
  • Writing dates as 15/03/2026 instead of 2026-03-15.
  • Forgetting that a pattern must match the whole value.
  • Putting facets in the wrong place, outside the restriction.
  • Using enumeration with different capital letters from the document. Values are case sensitive.
  • Expecting a type to reject a value that is correct in format but wrong in meaning.

Exercise

Write a schema element grade that accepts only the single letters A, B, C, D, or F.

Show solution
<xs:element name="grade">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="A"/>
      <xs:enumeration value="B"/>
      <xs:enumeration value="C"/>
      <xs:enumeration value="D"/>
      <xs:enumeration value="F"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

A pattern such as [ABCDF] would work too.

Knowledge check

1. In which order does xs:date expect a date?

Year, month, day, as in 2026-03-15.

2. Which facet limits a value to a list of choices?

enumeration

3. Why create a named simple type?

To reuse the same restriction in several places and change it in one spot.

Summary

  • XSD has built-in types for strings, numbers, booleans, dates, and more.
  • Restrict a type with facets such as ranges, lengths, enumeration, and pattern.
  • Name reusable types, and remember that types check format, not meaning.
Scroll to Top