9️⃣ 🔢 XSD Data Types – Define Structured XML Content with Precision
🧲 Introduction – Why Learn XSD Data Types?
XSD (XML Schema Definition) offers a wide range of built-in data types to define the content and constraints of XML documents. These types are essential for validating data formats, ensuring accuracy, and supporting structured data across systems. From strings and numbers to dates and booleans, XSD enforces strong typing in XML-based applications.
🎯 In this guide, you’ll learn:
- What data types are available in XSD
- How to use XSD to enforce correct formats (e.g., email, date, currency)
- Category-wise breakdown: string, numeric, date/time, and special types
- How to create type references and restrictions
📘 Topics Covered
🔢 Topic | 📄 Description |
---|---|
🔤 XSD String | String-based data types (e.g., xs:string , xs:token ) |
📅 XSD Date/Time | Temporal types like xs:date , xs:time , xs:dateTime |
🔢 XSD Numeric | Integer, decimal, float, and derived number types |
🧪 XSD Misc | Boolean, binary, QName, URI, ID, and other utility types |
🧷 XSD Reference | Creating and using named data type references |
🔤 XSD String Types
✅ Common String Types
Type | Description |
---|---|
xs:string | Basic text string |
xs:normalizedString | String with no line breaks or tabs |
xs:token | Removes extra spaces (used in formatted text) |
xs:language | Language code (e.g., en , fr ) |
xs:Name , xs:NCName | Valid XML names, with or without colons |
📄 Example:
<xs:element name="username" type="xs:string"/>
📅 XSD Date/Time Types
XSD includes rich date/time types for scheduling, logging, and history tracking.
Type | Format Example | Use Case |
---|---|---|
xs:date | 2025-06-23 | Birthdays, deadlines |
xs:time | 14:30:00 | Time of event |
xs:dateTime | 2025-06-23T14:30:00 | Log timestamps |
xs:duration | P2Y6M5DT12H35M30S | Subscription or countdown |
xs:gYear , xs:gMonth , etc. | Partial dates (year only, etc.) | Calendar components |
📄 Example:
<xs:element name="created" type="xs:dateTime"/>
🔢 XSD Numeric Types
These types allow for precise number representation and constraints.
✅ Integer Types
Type | Range |
---|---|
xs:integer | Unlimited whole numbers |
xs:int | −2,147,483,648 to 2,147,483,647 |
xs:long | Extended range |
xs:short , xs:byte | Smaller ranges |
xs:nonNegativeInteger | No negatives |
✅ Decimal Types
Type | Description |
---|---|
xs:decimal | Arbitrary precision floating-point |
xs:float | IEEE 32-bit floating-point |
xs:double | IEEE 64-bit floating-point |
📄 Example:
<xs:element name="price" type="xs:decimal"/>
🧪 XSD Miscellaneous Types
✅ Utility Types
Type | Description |
---|---|
xs:boolean | True/False (true , 1 , false , 0 ) |
xs:anyURI | Web addresses and URIs |
xs:base64Binary | Binary files encoded in base64 |
xs:hexBinary | Binary in hexadecimal |
xs:QName | Qualified XML names |
xs:ID , xs:IDREF | Unique identifiers in XML |
📄 Example:
<xs:element name="active" type="xs:boolean"/>
🧷 XSD Reference – Defining Reusable Types
Create your own reusable named data types:
✅ Define Once
<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>
✅ Use Anywhere
<xs:element name="status" type="StatusType"/>
📌 Summary – Recap & Next Steps
XSD provides a vast range of built-in data types to ensure that your XML data is consistent, valid, and reliable. From string formats to numerical precision and date/time standards, using the correct data type reduces parsing errors and improves data integrity across systems.
🔍 Key Takeaways:
- XSD data types enforce strict validation rules
- Choose types like
xs:string
,xs:decimal
,xs:dateTime
, etc., based on context - Use type references and custom restrictions for reusable patterns
⚙️ Real-World Relevance:
XSD data types are widely used in B2B communication, APIs, configuration files, and industry-standard XML formats like UBL, XBRL, and DocBook.
❓ FAQ – XSD Data Types
❓ What is the difference between xs:string
and xs:token
?
✅ xs:token
removes leading/trailing/extra spaces, making it ideal for formatted content.
❓ Can I validate email or URL formats in XSD?
✅ Yes, using xs:string
with a <pattern>
restriction that matches the desired format.
❓ What’s the difference between xs:decimal
and xs:float
?
✅ xs:decimal
provides arbitrary precision, while xs:float
is a binary floating-point type (less accurate for financial data).
❓ Are custom data types reusable in XSD?
✅ Absolutely! Define once using <xs:simpleType>
or <xs:complexType>
and reuse via the type
attribute.
Share Now :