XML Schema Simple Types are fundamental building blocks in XML Schema that define the content and constraints for individual elements or attributes in an XML document. They provide a way to specify the data type, format, and restrictions for values in XML elements.
Simple types serve several crucial functions in XML Schema:
To define a simple type in XML Schema, use the <xs:simpleType>
element. You can then specify restrictions using various facets.
<xs:simpleType name="ageType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
This example defines a simple type named "ageType" that restricts the value to integers between 0 and 120.
XML Schema Simple Types are widely used for various purposes:
Create specific data types tailored to your application's needs.
<xs:simpleType name="USStateCode">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z]{2}"/>
</xs:restriction>
</xs:simpleType>
Set limitations on acceptable values for elements or attributes.
<xs:simpleType name="percentageType">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
XML Schema provides numerous built-in simple types. Some commonly used ones include:
Type | Description |
---|---|
xs:string | Represents character strings |
xs:integer | Represents whole numbers |
xs:decimal | Represents decimal numbers |
xs:date | Represents dates (YYYY-MM-DD) |
xs:boolean | Represents true/false values |
XML Schema Simple Types are essential for defining and constraining data in XML documents. By mastering their usage, you can create more robust and reliable XML schemas, ensuring data consistency and validity across your XML-based applications.
For more advanced topics, explore XML Schema Complex Types and XML Schema Elements.