neildt
neildt

Reputation: 5362

How do I validate two dates in XSD

I have two date elements in my XSD file.

For example

<xs:element type="xs:date" name="DateFrom"/>
<xs:element type="xs:date" name="DateTo"/>

Basically, I'm wanting to check that the number of days between DateFrom and DateTo doesn't exceed 7 days.

I can do this check in my C# XML validation routine, but wondered can I do it in Xsd as well, and if so how ?

Upvotes: 1

Views: 673

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25054

In XSD 1.1 you can use assertions to check constraints like this; in XSD 1.0, you're out of luck.

[Addendum]: Another reader asks for a working example. Here is one.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"> 
  <xs:element name="DateRange">
    <xs:complexType> 
      <xs:sequence>
        <xs:element name="DateFrom" type="xs:date"/>
        <xs:element name="DateTo" type="xs:date"/>
      </xs:sequence>
      <xs:assert test="DateFrom lt DateTo"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

The schema described by this schema document accepts the following document.

<DateRange>
  <DateFrom>2011-01-01</DateFrom>
  <DateTo>2012-01-01</DateTo>
</DateRange>

It rejects the following document.

<DateRange>
  <DateFrom>2011-01-01</DateFrom>
  <DateTo>2010-01-01</DateTo>
</DateRange>

Upvotes: 2

Related Questions