Designing Your own XML Schema: Constraining with Restrictions - Restricting lengths in XML Schema
(Page 4 of 6 )
Let us consider that we would like to have any employee name restricted to fifty characters, with a minimum of four characters. The following complete schema would accomplish this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
<xs:element name="Employees">
<xs:annotation>
<xs:documentation>Contains All Employee information</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
<xs:minLength value="4" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="ID" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You need to observe the following fragment within the above complete schema, which actually does the restriction:
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
<xs:minLength value="4" />
</xs:restriction>
</xs:simpleType>
</xs:element>
The most important parts are the “maxLength” and “minLength” which handle our restriction.
Next: Restrictions based on lists (or enumerations) in XML Schema >>
More XML Articles
More By Jagadish Chaterjee