Examples of simpleTypes in an XML Schema

The W3C XML Schema Part 0: Primer specifies the following simpleType in its sample purchase order schema:

<!-- Stock Keeping Unit, a code for identifying products -->
               
<xsd:simpleType name="SKU"> 
               

              
<xsd:restriction base="xsd:string"> 
                   
<xsd:pattern value="\d{3}-[A-Z]{2}"/> 
                   
</xsd:restriction> 
                   

                
</xsd:simpleType>

This specifies that SKU is a simpleType. It is restricted to the values of the base type, which is xsd:string. This means that for a node that is of type SKU, the possible values are a subset of the values allowed for the xsd:string type.

The xsd:pattern element specifies that the pattern facet is being applied to the set of values allowed by the xsd:string type. The value of the xsd:pattern element is an XML Schema regular expression that specifies the allowable values for nodes of type SKU. In this example, the regular expression specifies that the value must be three digits, followed by a hyphen, followed by two uppercase ASCII letters - <xsd:pattern value="\d{3}-[A-Z]{2}"/>. For information about XML Schema expressions, see the W3C XML Schema Part 0: Primer.

Elsewhere in the purchase order schema, an attribute definition specifies that SKU is the type of its value:

<xsd:attribute name="partNum" type="SKU" use="required"/>
               

            

An XML document that uses a schema that contains this simpleType definition can specify the partNum attribute. The parser ensures that the value of the partNum attribute is in the range specified by the xsd:pattern element. The SKU type itself is not mentioned in the instance document.

Following is another example of a simpleType definition from the W3C XML Schema Part 0: Primer. This simpleType, myInteger, is based on the xsd:integer type. It specifies two facets ( minInclusive and maxInclusive), which specify the lower and upper inclusive bounds of the range of valid values.

<xsd:simpleType name="myInteger">
               

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

                
</xsd:simpleType>

 
Free Stylus Studio XML Training: