Restrict to specific dates in any year

50 Views Asked by At

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.

2

There are 2 best solutions below

1
Martin Honnen On BEST ANSWER

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.

0
Michael Kay On

I don't normally like using regular expressions for constraints on non-string types but in this case it's simple enough:

  <xs:simpleType name="end-of-quarter-date">
     <xs:restriction base="xs:date">
        <xs:pattern value="....-(03-31|06-30|09-30|12-31)"/>
     </xs:restriction>    
  </xs:simpleType>

and available in both XSD 1.0 and 1.1.