XML Schema Data Types

One of the biggest advantages XML Schema has over DTD is data types. DTD treats all content as text. XSD lets you declare that a price must be a decimal number, a date must follow the calendar format, and a quantity must be a positive integer. This prevents bad data from entering your system before processing even begins.

Two Categories of XSD Data Types

1. Simple Types

Simple types hold plain values — text, numbers, dates, booleans. They have no child elements and no attributes. Elements and attributes both use simple types.

2. Complex Types

Complex types contain child elements or attributes. They are covered in a separate topic. This page focuses entirely on simple types.

String Types

TypeDescriptionExample
xs:stringAny text, preserves all whitespaceHello World
xs:normalizedStringTabs and line breaks replaced with spacesHello World
xs:tokenNormalized, leading/trailing spaces removed, internal spaces collapsedHello World
xs:NameValid XML name (letters, digits, hyphens, underscores)product_id
xs:languageLanguage code per RFC 1766en, hi, fr

Numeric Types

TypeDescriptionExample
xs:integerAny whole number (positive or negative)-5, 0, 42
xs:positiveIntegerWhole numbers greater than zero1, 100, 9999
xs:negativeIntegerWhole numbers less than zero-1, -50
xs:nonNegativeIntegerZero and positive whole numbers0, 1, 500
xs:nonPositiveIntegerZero and negative whole numbers0, -1, -200
xs:decimalAny number with or without decimals3.14, 299.99
xs:float32-bit floating point number3.14e2
xs:double64-bit floating point number3.141592653
xs:byteInteger from -128 to 127100
xs:shortInteger from -32768 to 327671000
xs:intInteger from -2147483648 to 214748364750000
xs:longVery large integer9000000000

Date and Time Types

TypeFormatExample
xs:dateYYYY-MM-DD2024-03-15
xs:timeHH:MM:SS14:30:00
xs:dateTimeYYYY-MM-DDTHH:MM:SS2024-03-15T14:30:00
xs:durationPnYnMnDTnHnMnSP1Y6M (1 year, 6 months)
xs:gYearYYYY2024
xs:gMonth--MM--03 (March)
xs:gDay---DD---15 (15th day)
xs:gYearMonthYYYY-MM2024-03

Other Useful Types

TypeDescriptionExample
xs:booleanTrue or false valuetrue, false, 1, 0
xs:anyURIA URL or URIhttps://example.com
xs:base64BinaryBase64-encoded binary dataSGVsbG8=
xs:hexBinaryHexadecimal binary data48656C6C6F
xs:IDUnique identifier in documentP001
xs:IDREFReference to an xs:IDP001

Data Type Hierarchy Diagram

xs:anySimpleType
  |
  +-- xs:string
  |     |-- xs:normalizedString
  |     |     +-- xs:token
  |     |           +-- xs:language
  |     |           +-- xs:Name
  |     +-- xs:ID
  |     +-- xs:IDREF
  |
  +-- xs:decimal
  |     +-- xs:integer
  |           +-- xs:nonNegativeInteger
  |           |     +-- xs:positiveInteger
  |           |     +-- xs:unsignedLong → xs:unsignedInt → xs:unsignedShort → xs:unsignedByte
  |           +-- xs:nonPositiveInteger
  |                 +-- xs:negativeInteger
  |
  +-- xs:float
  +-- xs:double
  +-- xs:boolean
  +-- xs:date
  +-- xs:time
  +-- xs:dateTime
  +-- xs:anyURI

Using Data Types in a Schema

An online shopping order uses precise data types to prevent bad data:

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

  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="orderID"    type="xs:positiveInteger"/>
        <xs:element name="orderDate"  type="xs:date"/>
        <xs:element name="customerName" type="xs:string"/>
        <xs:element name="totalAmount" type="xs:decimal"/>
        <xs:element name="isPaid"     type="xs:boolean"/>
        <xs:element name="deliveryURL" type="xs:anyURI" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
Valid XML
<order>
  <orderID>5001</orderID>
  <orderDate>2024-03-15</orderDate>
  <customerName>Divya Krishnan</customerName>
  <totalAmount>2349.50</totalAmount>
  <isPaid>true</isPaid>
  <deliveryURL>https://track.store.com/5001</deliveryURL>
</order>
Invalid XML — wrong data type
<order>
  <orderID>five thousand</orderID>  <!-- not a positiveInteger -->
  <orderDate>15-03-2024</orderDate>  <!-- wrong date format -->
  <isPaid>yes</isPaid>               <!-- not a boolean -->
</order>

Why Data Types Matter

  • A price stored as a string allows "free" or "call for price" to pass validation. A price stored as xs:decimal only allows numbers.
  • A date stored as xs:string allows "tomorrow" or "soon." A date stored as xs:date requires the exact YYYY-MM-DD format.
  • A quantity stored as xs:positiveInteger blocks zero and negative values automatically.

Key Points to Remember

  • XML Schema supports dozens of built-in data types for precise content control.
  • xs:string accepts any text; xs:integer accepts only whole numbers.
  • xs:date requires the YYYY-MM-DD format; xs:dateTime adds time after a T separator.
  • xs:boolean accepts true, false, 1, or 0.
  • xs:anyURI validates web addresses and URIs.
  • Data types prevent invalid data long before it reaches application logic.

Leave a Comment

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