XML Tutorial
Estimated reading: 4 minutes 38 views

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 StringString-based data types (e.g., xs:string, xs:token)
📅 XSD Date/TimeTemporal types like xs:date, xs:time, xs:dateTime
🔢 XSD NumericInteger, decimal, float, and derived number types
🧪 XSD MiscBoolean, binary, QName, URI, ID, and other utility types
🧷 XSD ReferenceCreating and using named data type references

🔤 XSD String Types

✅ Common String Types

TypeDescription
xs:stringBasic text string
xs:normalizedStringString with no line breaks or tabs
xs:tokenRemoves extra spaces (used in formatted text)
xs:languageLanguage code (e.g., en, fr)
xs:Name, xs:NCNameValid 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.

TypeFormat ExampleUse Case
xs:date2025-06-23Birthdays, deadlines
xs:time14:30:00Time of event
xs:dateTime2025-06-23T14:30:00Log timestamps
xs:durationP2Y6M5DT12H35M30SSubscription 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

TypeRange
xs:integerUnlimited whole numbers
xs:int−2,147,483,648 to 2,147,483,647
xs:longExtended range
xs:short, xs:byteSmaller ranges
xs:nonNegativeIntegerNo negatives

✅ Decimal Types

TypeDescription
xs:decimalArbitrary precision floating-point
xs:floatIEEE 32-bit floating-point
xs:doubleIEEE 64-bit floating-point

📄 Example:

<xs:element name="price" type="xs:decimal"/>

🧪 XSD Miscellaneous Types

✅ Utility Types

TypeDescription
xs:booleanTrue/False (true, 1, false, 0)
xs:anyURIWeb addresses and URIs
xs:base64BinaryBinary files encoded in base64
xs:hexBinaryBinary in hexadecimal
xs:QNameQualified XML names
xs:ID, xs:IDREFUnique 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 :

Leave a Reply

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

Share

9️⃣ 🔢 XSD Data Types

Or Copy Link

CONTENTS
Scroll to Top