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
| Type | Description | Example |
|---|---|---|
| xs:string | Any text, preserves all whitespace | Hello World |
| xs:normalizedString | Tabs and line breaks replaced with spaces | Hello World |
| xs:token | Normalized, leading/trailing spaces removed, internal spaces collapsed | Hello World |
| xs:Name | Valid XML name (letters, digits, hyphens, underscores) | product_id |
| xs:language | Language code per RFC 1766 | en, hi, fr |
Numeric Types
| Type | Description | Example |
|---|---|---|
| xs:integer | Any whole number (positive or negative) | -5, 0, 42 |
| xs:positiveInteger | Whole numbers greater than zero | 1, 100, 9999 |
| xs:negativeInteger | Whole numbers less than zero | -1, -50 |
| xs:nonNegativeInteger | Zero and positive whole numbers | 0, 1, 500 |
| xs:nonPositiveInteger | Zero and negative whole numbers | 0, -1, -200 |
| xs:decimal | Any number with or without decimals | 3.14, 299.99 |
| xs:float | 32-bit floating point number | 3.14e2 |
| xs:double | 64-bit floating point number | 3.141592653 |
| xs:byte | Integer from -128 to 127 | 100 |
| xs:short | Integer from -32768 to 32767 | 1000 |
| xs:int | Integer from -2147483648 to 2147483647 | 50000 |
| xs:long | Very large integer | 9000000000 |
Date and Time Types
| Type | Format | Example |
|---|---|---|
| xs:date | YYYY-MM-DD | 2024-03-15 |
| xs:time | HH:MM:SS | 14:30:00 |
| xs:dateTime | YYYY-MM-DDTHH:MM:SS | 2024-03-15T14:30:00 |
| xs:duration | PnYnMnDTnHnMnS | P1Y6M (1 year, 6 months) |
| xs:gYear | YYYY | 2024 |
| xs:gMonth | --MM | --03 (March) |
| xs:gDay | ---DD | ---15 (15th day) |
| xs:gYearMonth | YYYY-MM | 2024-03 |
Other Useful Types
| Type | Description | Example |
|---|---|---|
| xs:boolean | True or false value | true, false, 1, 0 |
| xs:anyURI | A URL or URI | https://example.com |
| xs:base64Binary | Base64-encoded binary data | SGVsbG8= |
| xs:hexBinary | Hexadecimal binary data | 48656C6C6F |
| xs:ID | Unique identifier in document | P001 |
| xs:IDREF | Reference to an xs:ID | P001 |
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.
