Setting a Maximum Length

It is very common to see restrictions on the length of string Elements. To avoid truncating alphanumeric values before inserting them into a database, some programmers create schemas that make sure no one can use a string long enough to be truncated. To restrict our column lengths we can create a String1024Type type derived from string, as follows.

Setting a Maximum Length in SimpleCSV2.xsd
<xs:simpleType name="String1024Type">
  <xs:annotation>
    <xs:documentation>This user-defined type shows how we
        restrict our columns to 1024 characters
    </xs:documentation>
  </xs:annotation>
  <xs:restriction base="xs:string">
    <xs:maxLength value="1024"/>
  </xs:restriction>
</xs:simpleType>

We use the xs:simpleType Element with its name Attribute. The xs:restriction Element identifies the restriction base as the built-in string data type. We use the xs:maxLength Element as the constraining facet, with a value Attribute of 1024.

Our column Elements then are assigned this user-defined type instead of the built-in string type.

<xs:element name="Column01" type="String1024Type" minOccurs="0"/>

You can see this in context in SimpleCSV2.xsd.

I use a common convention of suffixing type names with Type. This is purely for readability. Schema language allows you to name types pretty much anything (well, anything allowable for NMTOKEN as defined in XML 1.0). A type may even have the same name as an Element that uses it. This is allowed because the names have different scope.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset