Re: custom date type in xml schema

Hi Hanna,

> I would like to ask how to customize date type such as MM-DD-CCYY,
> or YY/MM/DD instead of the default CCYY-MM-DD in schema. I have
> tried the following but the validation result is that the date value
> is invalid.

The only format that XML Schema allows for dates is CCYY-MM-DD. If you
want to use another format, you have to restrict the token type with a
regular expression, for example, here is a token that looks like a
date in the format DD-MM-CCYY:

<xs:simpleType name="mydatetype">
  <xs:restriction base="xs:token">
    <xs:pattern value="[0-9]{2}-[0-9]{2}-[0-9]{4}" />
  </xs:restriction>
</xs:simpleType>

Note that this doesn't test things like:

  - that the days range between 01 and 31 and the months between 01
    and 12
  - that the day that you have given for a month is valid (so you
    don't allow 31-04-2002)
  - that the day that you have given for February is valid, taking
    into account leap years (so you don't allow 29-02-2003, but do
    allow 29-02-2004)

You can't test all that with a regular expression, so if you want to
enforce valid dates you'll need to write some extra checking code.

Another strategy is to put your documents through a canonicalisation
process prior to validation, and during that canonicalisation process
reformat the dates from the readable date format that you want into
the standard date format that XML Schema recognises.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Thursday, 6 February 2003 05:15:24 UTC