My Technical Notes

Monday, 20 June 2016

XSD: Putting a MaxLength on a String element

Given a string element:


<xs:element name="Blah" type="xs:string" />

To turn it into a string with a max length of, say, `4000`, we turn it into:


<xs:element name="Blah">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:maxLength value="4000" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

If you wanted to define the restricted `simpleType` separately (let's say, for future changes), we could do:


<xs:simpleType name="longString">
  <xs:restriction base="xs:string">
    <xs:maxLength value="4000" />
  </xs:restriction>
</xs:simpleType>

and use it later in the file:


<xs:element name="Blah" type="longString" />

No comments: