XML Schema Simple Type Restrictions

beginner
11 min

XML Schema Simple Type Restrictions

Welcome to the world of XML Schema! In this tutorial, we'll delve into the fascinating world of Simple Type Restrictions. 🎯

What are 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. 📝

Getting Started

Before we dive into the exciting details, let's review the basic XML Schema building blocks:

  1. Element Declaration: Defines an XML element and its associated data type.
xml
<xs:element name="myElement" type="xs:string"/>
  1. Simple Type Definition: Creates a simple data type.
xml
<xs:simpleType name="mySimpleType"> <xs:restriction base="xs:string"> <!-- Simple Type Restrictions go here --> </xs:restriction> </xs:simpleType>

Common Simple Type Restrictions

Now that we've set the stage, let's explore some common Simple Type Restrictions.

📝 minLength: Minimum Length

Defines the minimum length of a string.

xml
<xs:element name="myElement" type="mySimpleType"/> <xs:simpleType name="mySimpleType"> <xs:restriction base="xs:string"> <xs:minLength value="4"/> </xs:restriction> </xs:simpleType>

📝 maxLength: Maximum Length

Defines the maximum length of a string.

xml
<xs:element name="myElement" type="mySimpleType"/> <xs:simpleType name="mySimpleType"> <xs:restriction base="xs:string"> <xs:maxLength value="10"/> </xs:restriction> </xs:simpleType>

📝 pattern: Regular Expression

Matches strings based on a regular expression.

xml
<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>

📝 minInclusive: Minimum Inclusive Value

Defines the minimum inclusive value for numeric and date types.

xml
<xs:element name="myElement" type="mySimpleType"/> <xs:simpleType name="mySimpleType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="5"/> </xs:restriction> </xs:simpleType>

📝 maxInclusive: Maximum Inclusive Value

Defines the maximum inclusive value for numeric and date types.

xml
<xs:element name="myElement" type="mySimpleType"/> <xs:simpleType name="mySimpleType"> <xs:restriction base="xs:integer"> <xs:maxInclusive value="10"/> </xs:restriction> </xs:simpleType>

Practical Example

Let's create a simple XML Schema for validating a product catalog containing product details.

xml
<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>

Quiz

Quick Quiz
Question 1 of 1

Which Simple Type Restriction is used to define the minimum inclusive value for numeric and date types?

Conclusion

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! 💡