tags:

views:

57

answers:

2

Hi

I am reading the tutorials on w3cschools ( http://www.w3schools.com/schema/schema_complex.asp ) but they don't seem to mention how you could add restrictions on complex types.

Like for instance I have this schema.

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

now I want to make sure the firstname is no more then 10 characters long. How do I do this?

I tried to put in the simple type for the firstname but it says I can't do that since I am using a complex type.

So how do I put restrictions like that on the file so the people who I give the schema to don't try to make the firstname 100 characters.

+3  A: 

There are some restrictions you can have with XSD:

Let's say you want firstName no more than 10 characters long. You will use something like:

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="10"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

For more complex constraints, you will have to do some code-based checking I guess.

Pablo Santa Cruz
As I was always putting "type="xs:string"" in the element just like it showed in a previous tutorial did not know you did not do that. So I think this solved my problem.
chobo2
+2  A: 
<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Is this solving your problem?

daniel