43

I have a question regarding adding restriction in my xml schema(xsd). I have a complex-type like:

<xsd:complexType name="xxx">
   <xsd:attribute/>
   .....
</xsd:complexType>

I have many attributes in this complex-type, and few of them are of string type. Now I want those string type attributes to be restricted to y no. of chars, how do I add this restriction?

Thanks! -Ankush

Gareth
  • 2,746
  • 4
  • 30
  • 44
Ankush
  • 787
  • 3
  • 12
  • 23

5 Answers5

74

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
34

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>
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 3
    the xs:length however requires the string to be the defined exact length - it doesn't just limit the maximum length. – marc_s Jun 08 '09 at 21:01
  • 2
    the xs:length is redundant, since the enumeration already defines all acceptable values as having three characters. –  Mar 30 '17 at 13:46
16

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>
Nemir
  • 283
  • 1
  • 3
  • 9
4

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 might want to consider explaining how to implement what you are suggesting with a code sample. Your answer is not clear at the moment – The Fabio Oct 06 '15 at 23:00
0

you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength.

rafalmag
  • 1,791
  • 1
  • 17
  • 24
Luixv
  • 8,590
  • 21
  • 84
  • 121