Welcome to the world of XML Schema! In this tutorial, we'll delve into the fascinating world of Simple Type Restrictions. 🎯
In XML Schema, Simple Type Restrictions allow us to impose constraints on data types. They help ensure the quality and integrity of data by limiting the range of valid values that can be used within an XML document. 📝
Before we dive into the exciting details, let's review the basic XML Schema building blocks:
<xs:element name="myElement" type="xs:string"/><xs:simpleType name="mySimpleType">
<xs:restriction base="xs:string">
<!-- Simple Type Restrictions go here -->
</xs:restriction>
</xs:simpleType>Now that we've set the stage, let's explore some common Simple Type Restrictions.
Defines the minimum length of a string.
<xs:element name="myElement" type="mySimpleType"/>
<xs:simpleType name="mySimpleType">
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
</xs:restriction>
</xs:simpleType>Defines the maximum length of a string.
<xs:element name="myElement" type="mySimpleType"/>
<xs:simpleType name="mySimpleType">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>Matches strings based on a regular expression.
<xs:element name="myElement" type="mySimpleType"/>
<xs:simpleType name="mySimpleType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{4}"/> <!-- matches 4-digit numbers -->
</xs:restriction>
</xs:simpleType>Defines the minimum inclusive value for numeric and date types.
<xs:element name="myElement" type="mySimpleType"/>
<xs:simpleType name="mySimpleType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="5"/>
</xs:restriction>
</xs:simpleType>Defines the maximum inclusive value for numeric and date types.
<xs:element name="myElement" type="mySimpleType"/>
<xs:simpleType name="mySimpleType">
<xs:restriction base="xs:integer">
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>Let's create a simple XML Schema for validating a product catalog containing product details.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="productCatalog">
<xs:complexType>
<xs:sequence>
<xs:element name="product" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="productId" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Simple Type Restrictions -->
<xs:simpleType name="productIdType">
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="priceType">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0.01"/>
<xs:maxInclusive value="9999999.99"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>Which Simple Type Restriction is used to define the minimum inclusive value for numeric and date types?
By now, you've gained a solid understanding of XML Schema Simple Type Restrictions. These powerful tools allow us to create well-structured and validated XML documents. ✅
Stay tuned for more exciting lessons on XML Schema! Happy learning! 💡