Usman
Usman

Reputation: 2890

How can I restrict the gYear of date in xsd be only between 1920-2000?

I want to restrict the user from entering the year part of the date outside the range of (1920-2000). The year should lie between this interval, I don't want to restrict the particular date, however I want the YEAR part of the date to lie within this range.

What should I do in the XSD?

Right now I am doing this, but it is creating a problem and the XML is not validating properly.

    <xs:element name="dateOfBirth">    
     <xs:complexType>
      <xs:sequence>
        <xs:element name="Year">
        <!--Restriction applied : Year value can lie between 1920-2000-->
            <xs:simpleType>
                <xs:restriction base="xs:gYear">
                    <xs:minInclusive value="1920"/>
                    <xs:maxInclusive value="2000"/>
                </xs:restriction>       
            </xs:simpleType>
        <!--Restriction applied : Year value can lie between 1920-2000-->
    </xs:element>
    <xs:element name="-" type="xs:char"/>
            <xs:element name="Month">

        <!--Restriction applied : Month value can lie between 1-12-->
        <xs:simpleType>
            <xs:restriction base="xs:gMonth">
                <xs:minInclusive value="1"/>
                <xs:maxInclusive value="12"/>
            </xs:restriction>       
        </xs:simpleType>

        <!--Restriction applied : Month value can lie between 1-12-->
        </xs:element>
          <xs:element name="-" type="xs:char"/>     
          <xs:element name="Day">
        <!--Restriction applied : Day value can lie between 1-31-->
            <xs:simpleType>
                <xs:restriction base="xs:gDay">
                        <xs:minInclusive value="1"/>
                        <xs:maxInclusive value="31"/>
                </xs:restriction>       
            </xs:simpleType>
        <!--Restriction applied : Day value can lie between 1-31-->   
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>

Upvotes: 1

Views: 10044

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

You're seriously confused.

Your response to Wellens indicates that your instance data is of the form <DOB>1988-01-24</DOB>. That's a value of type xs:date. The element must be described in the schema as a simple type, not as a complex type whose structure is a sequence of (child) elements. If it were in the form

<DOB><year>1988</year><month>01</month><day>24</day></DOB>

then you could use a complex type, whose definition is a sequence comprising an element of type gYear, an element of type gMonth, and an element of type gDay. But I don't think your data is like this.

Your confusion is particularly evident where you've tried to define an element whose name is "-". That's not even a valid XML element name, let alone an element name that appears in your data.

So, how do you restrict xs:date to be in the range 1920-01-01 to 2000-12-31, say? The answer is to use

<xs:simpleType>
  <xs:restriction base="xs:date">
    <xs:minInclusive value="1920-01-01"/>
    <xs:maxInclusive value="2000-12-31"/>
    <xs:pattern value=".{10}"/>
  </xs:restriction>
</xs:simpleType>

The xs:pattern restricts the value to being a 10-character string. This prevents a timezone being appended, such as 1920-01-01Z.

Upvotes: 3

Steve Wellens
Steve Wellens

Reputation: 20620

Maybe you need to add the base type of integer as below:

 <xs:element name="age">
   <xs:simpleType>
     <xs:restriction base="xs:integer">
       <xs:minInclusive value="0"/>
       <xs:maxInclusive value="100"/>
     </xs:restriction>
   </xs:simpleType>
 </xs:element> 

Otherwise you are probably doing ascii compares instead of integer compares.

Upvotes: 3

Related Questions