xmldatevalidationxsdxsd-validation

Restrict to specific dates in any year


I am creating a schema that requires a date that corresponds to the end of a quarterly period (so YYYY-03-31, YYYY-06-30, YYYY-09-30, and YYYY-12-31).

<xs:element type="xs:date" name="FilingPeriod"/>

Is there a sensible way to accept these values in date format and restrict them to these specific but potentially infinite values (i.e., any year forever)? I imagine I could put some restrictions on a string field and validate that way, but that seems like a hack around using the proper date format.


Solution

  • XSD 1.1 with assertion on simple type:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      
      <xs:element name="dates">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="date" type="end-of-quarter-date" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      
      <xs:simpleType name="end-of-quarter-date">
        <xs:restriction base="xs:date">
          <xs:assertion 
            test="(day-from-date($value) = 31 and month-from-date($value) = (3, 12))
                  or
                  (day-from-date($value) = 30 and month-from-date($value) = (6, 9))"/>
        </xs:restriction>    
      </xs:simpleType>
    
    </xs:schema>
    

    Online XSD fiddle in XML workbench.