XML - XML Schema Datatypes

What XML Schema Datatypes Are

  • XML Schema (XSD) defines the structure, content, and data types of XML documents.

  • Datatypes specify what kind of values an XML element or attribute can hold (e.g., numbers, text, dates).

  • Using datatypes ensures that XML data is consistent, valid, and type-safe.


Common XML Schema Datatypes

  1. String

    • Represents text data.

    • Can contain letters, numbers, and symbols.

    • Example:

      <name>John Doe</name>
      
    • XSD declaration:

      <xs:element name="name" type="xs:string"/>
      
  2. Integer

    • Represents whole numbers (positive, negative, or zero).

    • Example:

      <age>30</age>
      
    • XSD declaration:

      <xs:element name="age" type="xs:integer"/>
      
  3. Decimal / Float / Double

    • Represents numbers with fractions (decimal points).

    • Useful for prices, measurements, or scientific data.

    • Example:

      <price>19.99</price>
      
    • XSD declaration:

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

    • Represents true/false values.

    • Example:

      <available>true</available>
      
    • XSD declaration:

      <xs:element name="available" type="xs:boolean"/>
      
  5. Date and Time

    • Includes several types: xs:date, xs:time, xs:dateTime.

    • Example:

      <dob>1990-05-15</dob>
      <meeting>2025-10-08T14:30:00</meeting>
      
    • XSD declaration:

      <xs:element name="dob" type="xs:date"/>
      <xs:element name="meeting" type="xs:dateTime"/>
      
  6. Other Useful Types

    • xs:ID → unique identifier within a document.

    • xs:IDREF → references an ID elsewhere.

    • xs:enumeration → restricts values to a predefined set.

      <xs:simpleType name="colorType">
        <xs:restriction base="xs:string">
          <xs:enumeration value="red"/>
          <xs:enumeration value="green"/>
          <xs:enumeration value="blue"/>
        </xs:restriction>
      </xs:simpleType>
      

Benefits of Using XML Schema Datatypes

  • Validation – Ensures XML elements contain the correct type of data.

  • Consistency – Prevents invalid or unexpected values.

  • Interoperability – Standard types are recognized across systems and programming languages.


In Short

XML Schema datatypes define the kind of content XML elements and attributes can have, such as text, numbers, dates, or booleans. They make XML more structured, reliable, and easy to validate, especially when exchanging data between applications.