🔢 XSD Numeric – Define and Restrict Numbers in XML Schema
🧲 Introduction – Why Use XSD Numeric Types?
Numbers are everywhere in XML documents—prices, quantities, IDs, percentages, coordinates, and more. XML Schema (XSD) provides rich support for numeric data types, including integers, decimals, floating points, and constraints. With XSD, you can precisely validate numeric values, set ranges, and enforce precision.
🎯 In this guide, you’ll learn:
- Common numeric types in XSD (
xs:integer
,xs:decimal
,xs:float
, etc.) - How to define numeric elements and attributes
- How to apply constraints like
minInclusive
,maxExclusive
,totalDigits
, andfractionDigits
- Real-world examples for price tags, scores, and limits
📘 Common XSD Numeric Types
Type | Description | Example |
---|---|---|
xs:integer | Whole numbers (no decimals) | 42 |
xs:decimal | Arbitrary-precision decimal numbers | 12.34 |
xs:float | 32-bit floating-point number (approximate) | 3.14E2 |
xs:double | 64-bit floating-point number | 1.7976931348623157E308 |
xs:nonNegativeInteger | Whole numbers ≥ 0 | 0 , 99 |
xs:positiveInteger | Whole numbers > 0 | 1 , 5000 |
xs:nonPositiveInteger | Whole numbers ≤ 0 | 0 , -5 |
xs:negativeInteger | Whole numbers < 0 | -1 , -999 |
🧾 Example – Basic Numeric Element
<xs:element name="price" type="xs:decimal"/>
✅ Valid XML:
<price>149.99</price>
🔐 Restricting Numeric Values
🔹 Example – Integer Range (Age)
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Accepts values between 0
and 120
.
🔹 Example – Decimal Precision (Money)
<xs:element name="price">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="6"/>
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Allows prices up to 9999.99
with exactly 2 decimal places.
➕ Numeric Types in Attributes
<xs:attribute name="quantity" type="xs:nonNegativeInteger" use="required"/>
✅ Valid:
<item quantity="10"/>
🧠 Choosing Between decimal
, float
, and double
Use Case | Recommended Type | Reason |
---|---|---|
Prices, currency | xs:decimal | Exact precision with fixed-point support |
Measurements | xs:float or xs:double | For approximate values, scientific data |
Whole numbers | xs:integer or derived types | Enforces integer-only inputs |
✅ Best Practices for Numeric Types
- ✔️ Use
xs:decimal
for financial data - ✔️ Use restrictions like
minInclusive
/maxExclusive
for ranges - ✔️ Use
totalDigits
andfractionDigits
for precision control - ✔️ Use derived types like
positiveInteger
for business logic - ❌ Don’t use
xs:float
/xs:double
for exact currency or IDs—rounding may occur - ❌ Avoid vague patterns like
xs:string
for numeric values—use real numeric types
📌 Summary – Recap & Next Steps
XSD numeric types enable robust, type-safe validation of numbers in XML. From quantities and scores to money and temperature, they ensure your values meet format, range, and accuracy expectations.
🔍 Key Takeaways:
- Use
xs:integer
,xs:decimal
,xs:float
, andxs:double
for numeric data - Apply range (
minInclusive
,maxExclusive
) and precision (totalDigits
) constraints - Choose types based on whether accuracy or approximation is needed
⚙️ Real-world relevance: Used in e-commerce product data, APIs, statistics, invoices, performance reports, and system metrics.
❓ FAQs – XSD Numeric
❓ What’s the difference between decimal
and float
?
✅ decimal
is exact and ideal for currency; float
is approximate (scientific).
❓ Can I enforce a specific number of decimal places?
✅ Yes. Use fractionDigits
.
❓ What happens if a value exceeds totalDigits?
✅ It will fail schema validation.
❓ Can I make a number field optional?
✅ Yes, set minOccurs="0"
or use="optional"
(for attributes).
❓ Can numeric types be negative by default?
✅ Yes, unless you use a non-negative or positive variant.
Share Now :