简体   繁体   中英

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 ?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM