Xsd Numeric in XML
? XSD Numeric Types in XML Schema
In XSD (XML Schema Definition), there are several built-in numeric data types you can use to define elements or attributes that must hold numbers. These help validate that the data is numeric and can also enforce constraints like minimum and maximum values.
? Common Numeric Types in XSD
| XSD Type | Description | Example Value |
|---|---|---|
xs:integer | Any integer (positive, negative, zero) | 42, -7 |
xs:decimal | Decimal number (floating point) | 3.14, 100.00 |
xs:float | 32-bit floating point | 1.23E4 |
xs:double | 64-bit floating point | 2.718281828 |
xs:nonNegativeInteger | Integer ? 0 | 0, 25 |
xs:positiveInteger | Integer > 0 | 1, 99 |
xs:nonPositiveInteger | Integer ? 0 | 0, -5 |
xs:negativeInteger | Integer < 0 | -1, -20 |
? Example: Using Numeric Types in XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="product"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:integer"/> <xs:element name="price" type="xs:decimal"/> <xs:element name="quantity" type="xs:nonNegativeInteger"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>? Example XML Valid for Above XSD
<product> <id>1001</id> <price>19.99</price> <quantity>5</quantity></product>? Adding Numeric Restrictions (Min/Max)
You can restrict numeric ranges like this:
<xs:simpleType name="AgeType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction></xs:simpleType><xs:element name="age" type="AgeType"/>This limits age to between 0 and 120 inclusive.
Summary
Use xs:integer or xs:decimal for general numbers.
Use derived types like xs:nonNegativeInteger to restrict the range.
Use restrictions to set min/max values.
Numeric types enable strong validation in your XML documents.
Want me to help create a full XML + XSD example demonstrating numeric types and restrictions?