Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xsd

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

like image 624
Ankush Avatar asked Jun 08 '09 20:06

Ankush


People also ask

What does complexType mean in XSD?

Definition and Usage The complexType element defines a complex type. A complex type element is an XML element that contains other elements and/or attributes.

What is minOccurs and maxOccurs in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is complexType and simpleType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.


1 Answers

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

like image 124
marc_s Avatar answered Nov 09 '22 16:11

marc_s