XSD: how to restrict number of characters in string type attribute?

Ankush picture Ankush · Jun 8, 2009 · Viewed 97.9k times · Source

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

Answer

marc_s picture marc_s · Jun 8, 2009

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