XSD Data Types

Understanding built-in and custom data types in XML Schema

🔢 What are XSD Data Types?

XSD data types define what kind of data an element or attribute can contain. They ensure data validity and include built-in types like string, integer, date, and boolean.


<!-- Using data types -->
<xs:element name="age" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
                                    

Built-in Data Types

📝

String Types

Text and character data

<xs:element name="text" 
  type="xs:string"/>
🔢

Numeric Types

Integer and decimal numbers

<xs:element name="count" 
  type="xs:integer"/>
📅

Date/Time Types

Dates, times, and durations

<xs:element name="birthday" 
  type="xs:date"/>

Boolean Type

True or false values

<xs:element name="active" 
  type="xs:boolean"/>

🔹 String Data Types

String types handle text data with various formats and constraints. Use them for names, descriptions, and any textual content in your XML documents.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Basic string -->
  <xs:element name="name" type="xs:string"/>
  
  <!-- Normalized string (no line breaks/tabs) -->
  <xs:element name="address" type="xs:normalizedString"/>
  
  <!-- Token (no extra whitespace) -->
  <xs:element name="code" type="xs:token"/>
  
</xs:schema>

<!-- Valid XML -->
<name>John Doe</name>
<address>123 Main Street</address>
<code>ABC123</code>

String Type Differences:

string: Allows all characters including whitespace

normalizedString: Replaces tabs/newlines with spaces

token: Removes leading/trailing spaces and collapses whitespace

🔹 Numeric Data Types

Numeric types handle different kinds of numbers from whole integers to precise decimals. Choose the appropriate type based on your data requirements.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Integer (whole numbers) -->
  <xs:element name="quantity" type="xs:integer"/>
  
  <!-- Positive integer -->
  <xs:element name="id" type="xs:positiveInteger"/>
  
  <!-- Decimal (with fractions) -->
  <xs:element name="price" type="xs:decimal"/>
  
  <!-- Float (scientific notation) -->
  <xs:element name="measurement" type="xs:float"/>
  
  <!-- Double (larger range) -->
  <xs:element name="distance" type="xs:double"/>
  
</xs:schema>

<!-- Valid XML -->
<quantity>100</quantity>
<id>42</id>
<price>19.99</price>
<measurement>3.14159</measurement>
<distance>1.5E8</distance>

Numeric Type Variants:

  • integer: ..., -2, -1, 0, 1, 2, ...
  • positiveInteger: 1, 2, 3, ...
  • negativeInteger: ..., -3, -2, -1
  • nonNegativeInteger: 0, 1, 2, ...
  • nonPositiveInteger: ..., -2, -1, 0

🔹 Date and Time Types

Date and time types handle temporal data with specific formats. These types ensure consistent date/time representation across your XML documents.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Date (YYYY-MM-DD) -->
  <xs:element name="birthDate" type="xs:date"/>
  
  <!-- Time (HH:MM:SS) -->
  <xs:element name="startTime" type="xs:time"/>
  
  <!-- DateTime (YYYY-MM-DDTHH:MM:SS) -->
  <xs:element name="timestamp" type="xs:dateTime"/>
  
  <!-- Duration (PnYnMnDTnHnMnS) -->
  <xs:element name="duration" type="xs:duration"/>
  
</xs:schema>

<!-- Valid XML -->
<birthDate>1990-05-15</birthDate>
<startTime>14:30:00</startTime>
<timestamp>2024-10-16T14:30:00</timestamp>
<duration>P1Y2M3DT4H5M6S</duration>

Format Examples:

date: 2024-10-16

time: 14:30:00

dateTime: 2024-10-16T14:30:00

duration: P1Y2M (1 year, 2 months)

🔹 Boolean and Other Types

Additional data types for specific use cases including boolean values, binary data, and URIs:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Boolean (true/false or 1/0) -->
  <xs:element name="isActive" type="xs:boolean"/>
  
  <!-- anyURI (web addresses) -->
  <xs:element name="website" type="xs:anyURI"/>
  
  <!-- base64Binary (encoded binary data) -->
  <xs:element name="image" type="xs:base64Binary"/>
  
  <!-- hexBinary (hex encoded data) -->
  <xs:element name="hash" type="xs:hexBinary"/>
  
</xs:schema>

<!-- Valid XML -->
<isActive>true</isActive>
<website>https://example.com</website>
<image>R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==</image>
<hash>48656C6C6F</hash>

🔹 Custom Data Types

Create your own data types by restricting or extending built-in types. This allows you to define specific validation rules for your data.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Custom age type (0-120) -->
  <xs:simpleType name="AgeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="120"/>
    </xs:restriction>
  </xs:simpleType>
  
  <!-- Custom email type -->
  <xs:simpleType name="EmailType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[^@]+@[^@]+\.[^@]+"/>
    </xs:restriction>
  </xs:simpleType>
  
  <!-- Custom status type (enumeration) -->
  <xs:simpleType name="StatusType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="active"/>
      <xs:enumeration value="inactive"/>
      <xs:enumeration value="pending"/>
    </xs:restriction>
  </xs:simpleType>
  
  <!-- Using custom types -->
  <xs:element name="age" type="AgeType"/>
  <xs:element name="email" type="EmailType"/>
  <xs:element name="status" type="StatusType"/>
  
</xs:schema>

<!-- Valid XML -->
<age>25</age>
<email>[email protected]</email>
<status>active</status>

🔹 Complete Example: User Profile

A practical example using multiple data types:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="user">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="username" type="xs:string"/>
        <xs:element name="email" type="xs:string"/>
        <xs:element name="age" type="xs:positiveInteger"/>
        <xs:element name="registrationDate" type="xs:date"/>
        <xs:element name="isVerified" type="xs:boolean"/>
        <xs:element name="balance" type="xs:decimal"/>
        <xs:element name="website" type="xs:anyURI" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:integer" use="required"/>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

<!-- Valid XML -->
<user id="12345">
  <username>johndoe</username>
  <email>[email protected]</email>
  <age>30</age>
  <registrationDate>2024-01-15</registrationDate>
  <isVerified>true</isVerified>
  <balance>150.75</balance>
  <website>https://johndoe.com</website>
</user>

🧠 Test Your Knowledge

Which data type would you use for a price with decimal places?