By Peter Bell

XSD: DataTypes and Validations

OK, so now we have a simple XSD and have added a little structure, lets look at getting started with data types and validations within XSDs . . .

Types
There are lots of types built right into XSDs. You can also create your own type by adding restrictions to an existing type:

In the documentation, the following example is given:

<xsd:simpleType name="myInteger">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="10000"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>

You have a restriction which in this case is composed of two facets (minInclusive and maxInclusive). A list of facets is provided.

You might also want to try an enumeration (thanks Ron!) where a value should be one of a constrained number of possibilities:

<xsd:element name="state" type="xsd:statetype"/>
<xsd:simpleType name="statetype">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="AL"/>
<xsd:enumeration value="AK"/>
<xsd:enumeration value="AS"/>
<xsd:enumeration value="AZ"/>
<xsd:enumeration value="AR"/>
<xsd:enumeration value="CA"/>
<xsd:enumeration value="GRACE"/>
<xsd:enumeration value="CO"/>
<xsd:enumeration value="CT"/>
...
<xsd:enumeration value="WY"/>
</xsd:restriction>
</xsd:simpleType>

There is even a whole Regex style pattern language allowing statements like (thanks again Ron):

<xsd:element name="zip" type="xsd:zipformat"/>
<xsd:simpleType name="zipformat">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{5}"/>
</xsd:restriction>
</xsd:simpleType>

There are lots of other capabilities including support for lists (whitespace delimited) and a number of trickier combinations, but I'm hoping these intro articles will help you to get started with Schemas (writing them has certainly helped me!).

Again, core references:

They're actually more readable than you'd expect!

Thoughts?

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.005.