XML Schema Tutorial



XSD Restrictions


Restrictions declare the acceptable values for elements or attributes.



Number Range

 <xs:element name="level">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

xs:minExclusive, xs:maxExclusive can be used instead. (excluding the specified value itself)



Simple Type Declaration

The type can be declared by xs:simpleType and refered by an element or attribute.

 <xs:element name="level" type="levelType"/>
<xs:simpleType name="levelType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>


Enumeration

 <xs:simpleType name="itemType">
<xs:restriction base="xs:string">
<xs:enumeration value="weapon"/>
<xs:enumeration value="armor"/>
</xs:restriction>
</xs:simpleType>


Regular Expression (xs:pattern)

 <xs:simpleType name="lowerCaseString">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]*"/>
</xs:restriction>
</xs:simpleType>


String Length

 <xs:simpleType name="id">
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
<xs:maxLength value="16"/>
</xs:restriction>
</xs:simpleType>

xs:length specifies minLength and maxLength as the same value.



Whitespace

 <xs:simpleType name="id">
<xs:restriction base="xs:string">
<xs:whitespace value="collapse"/>
</xs:restriction>
</xs:simpleType>

  • preserve : preserve white space characters
  • replace : replace tabs, line feeds, carriage returns with space characters.
  • collpase : remove tabs, line feeds, carriage returns, space characters except for space characters between other characters.


Other Restrictions

  • xs:totalDigits : the number of digits (greater than 0)
  • xs:fractionDigits : the number of decimal places (greater than or equal to 0)