I have a question regarding adding restriction in my xml schema(xsd). I have a complex-type like:
you can always define the maximal length of a string in xsd.
Just add the attribute maxLength
resp. minLength
.
You need to create a simple type like so:
<xs:simpleType name="LimitedString">
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
and then use this new type in your schema:
<xs:complexType name="test">
<xs:sequence>
<xs:element name="abc" type="xs:String" />
</xs:sequence>
<xs:attribute type="LimitedString" name="myattr" />
</xs:complexType>
Marc
The answer by marc_s would require you to define a new type for every possible string length you might use. Instead you can define the restriction directly on the attribute itself.
<xs:complexType name="Test">
<xs:attribute name="LimitedString">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value = "50"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
Add this and it worked for me,
<xs:element name="vendor">
<xs:simpleType>
<xs:annotation>
<xs:documentation>80-char strings only</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:maxLength value="80"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
You can restrict the string to a number of chars like this:
<xs:simpleType name="threeCharString">
<xs:annotation>
<xs:documentation>3-char strings only</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:length value="3"/>
</xs:restriction>
</xs:simpleType>
The xs:length in the above limits the length of the string to exactly 3 chars. You can also use xs:minLength and xs:maxlength, or both.
You can provide a pattern like so:
<xs:simpleType name="fourCharAlphaString">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]{4}"/>
</xs:restriction>
</xs:simpleType>
The above says, 4 chars, of any of a-z, A-Z. The xs:pattern is a regular expression, so go to town with it.
You can restrict the string to a particular set of strings in this way:
<xs:simpleType name="iso3currency">
<xs:annotation>
<xs:documentation>ISO-4217 3-letter currency codes. Only a subset are defined here.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:length value="3"/>
<xs:enumeration value="AUD"/>
<xs:enumeration value="BRL"/>
<xs:enumeration value="CAD"/>
<xs:enumeration value="CNY"/>
<xs:enumeration value="EUR"/>
<xs:enumeration value="GBP"/>
<xs:enumeration value="INR"/>
<xs:enumeration value="JPY"/>
<xs:enumeration value="RUR"/>
<xs:enumeration value="USD"/>
</xs:restriction>
</xs:simpleType>