Reputation: 21
I have defined the following simple type in a XSD:
<xs:simpleType name="myDateFormat">
<xs:union memberTypes="xs:dateTime xs:date xs:gYearMonth xs:gYear"/>
</xs:simpleType>
I would like to allow YYYY, YYYY-MM, YYYY-MM-DD and full datetime formats. But after doing some testing I have realised that I don't get an error with the following values:
How can I create a type that allows only YYYY, YYYY-MM, YYYY-MM-DD and full datetime?
EDIT1: Is this a possible solution?
<xs:simpleType name="myDateFormat">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:gYear">
<xs:pattern value="\d{4}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:gYearMonth">
<xs:pattern value="\d{4}-\d{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:date">
<xs:pattern value="\d{4}-\d{2}-\d{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:dateTime">
<xs:pattern value="\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
Upvotes: 2
Views: 950
Reputation: 111491
It's the xs:gYear
that's allowing the unwanted values. (It's allowing them as years with more than 4 digits.)
You can define your own year that's limited to 4 digits:
<xs:simpleType name="gYear4">
<xs:restriction base="xs:nonNegativeInteger">
<xs:totalDigits value="4"/>
</xs:restriction>
</xs:simpleType>
And use that instead in your xs:union
:
<xs:simpleType name="myDateFormat">
<xs:union memberTypes="xs:dateTime xs:date xs:gYearMonth gYear4"/>
</xs:simpleType>
And then the values you want will be allowed, and the YYYY[YYYYYY]
values that you do not will be prohibited, as requested.
Upvotes: 2