XSD Numeric Data Types

Welcome to The Coding College, your one-stop destination for programming tutorials and guides! In this post, we’ll explore XSD Numeric Data Types—a vital part of XML Schema (XSD) that helps define and validate numerical data in XML documents.

What Are XSD Numeric Data Types?

XSD Numeric Data Types are used to define elements or attributes that hold numeric values in XML. These data types provide precision, support constraints, and ensure that your numerical data adheres to specific formats or ranges.

Types of XSD Numeric Data Types

Data TypeDescription
xs:decimalRepresents decimal numbers with arbitrary precision.
xs:integerRepresents whole numbers (subtype of xs:decimal).
xs:nonPositiveIntegerRepresents integers less than or equal to 0.
xs:negativeIntegerRepresents integers less than 0.
xs:nonNegativeIntegerRepresents integers greater than or equal to 0.
xs:positiveIntegerRepresents integers greater than 0.
xs:longRepresents long integers (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
xs:intRepresents standard integers (-2,147,483,648 to 2,147,483,647).
xs:shortRepresents short integers (-32,768 to 32,767).
xs:byteRepresents very small integers (-128 to 127).
xs:unsignedLongRepresents positive long integers (0 to 18,446,744,073,709,551,615).
xs:unsignedIntRepresents positive integers (0 to 4,294,967,295).
xs:unsignedShortRepresents positive short integers (0 to 65,535).
xs:unsignedByteRepresents positive small integers (0 to 255).
xs:floatRepresents single-precision floating-point numbers.
xs:doubleRepresents double-precision floating-point numbers.

Examples of XSD Numeric Data Types

1. Defining a Decimal Number

XSD Schema

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

Valid XML

<price>123.45</price>

Invalid XML

<price>123.45.67</price> <!-- Invalid decimal format -->

2. Defining an Integer

XSD Schema

<xs:element name="quantity" type="xs:integer"/>

Valid XML

<quantity>10</quantity>

Invalid XML

<quantity>10.5</quantity> <!-- Fractional values are not allowed -->

3. Using xs:positiveInteger

XSD Schema

<xs:element name="age" type="xs:positiveInteger"/>

Valid XML

<age>25</age>

Invalid XML

<age>-5</age> <!-- Negative numbers are not allowed -->

4. Using xs:nonNegativeInteger

XSD Schema

<xs:element name="distance" type="xs:nonNegativeInteger"/>

Valid XML

<distance>0</distance>
<distance>150</distance>

Invalid XML

<distance>-10</distance> <!-- Negative numbers are not allowed -->

5. Defining a Floating-Point Number

XSD Schema

<xs:element name="temperature" type="xs:float"/>

Valid XML

<temperature>36.6</temperature>
<temperature>1.23E4</temperature> <!-- Scientific notation is valid -->

Invalid XML

<temperature>36,6</temperature> <!-- Commas are not allowed -->

6. Restricting a Numeric Range

XSD Schema

<xs:element name="score">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="100"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Valid XML

<score>85</score>

Invalid XML

<score>120</score> <!-- Exceeds the maximum value -->
<score>-5</score> <!-- Below the minimum value -->

Using Facets with Numeric Data Types

Facets allow you to impose additional restrictions on numeric data types.

1. Enforcing a Pattern

XSD Schema

<xs:element name="productCode">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:pattern value="\d{5}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

This enforces a 5-digit numeric product code.

Valid XML

<productCode>12345</productCode>

Invalid XML

<productCode>1234</productCode> <!-- Not 5 digits -->

2. Enforcing Specific Values (Enumeration)

XSD Schema

<xs:element name="rating">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:enumeration value="1"/>
      <xs:enumeration value="2"/>
      <xs:enumeration value="3"/>
      <xs:enumeration value="4"/>
      <xs:enumeration value="5"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

This allows ratings from 1 to 5 only.

Valid XML

<rating>3</rating>

Invalid XML

<rating>6</rating> <!-- Not in the allowed range -->

When to Use XSD Numeric Data Types

  1. E-commerce: Price, quantity, discounts, etc.
  2. Healthcare: Patient vitals like temperature, weight, blood pressure.
  3. Finance: Account balances, interest rates, etc.
  4. Education: Test scores, grades, etc.

Conclusion

XSD Numeric Data Types offer flexibility and precision for validating numerical data in XML documents. Whether you’re handling whole numbers, decimals, or floating-point numbers, these data types help ensure the consistency and accuracy of your XML data.

Stay tuned to The Coding College for more in-depth programming tutorials and tips!

Leave a Comment