Re: custom date type in xml schema

On Thu, 2003-02-06 at 10:15, Jeni Tennison wrote:

> <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.

I'm being tedious, but you can:

(?:(?:(?:[02468][048]|[13579][26])00|\d\d(?:[02468][48]|[2468][048]|
[13579][26]))02(?:0[1-9]|[12]\d)|\d{4}(?:02(?:0[1-9]|1\d|2[0-8])|(?:
(?:0[469]|11)(?:0[1-9]|[12]\d|30))|(?:0[13578]|1[02])(?:0[1-9]|[12]
\d|3[0-1])))

:-)

That uses some Perl-isms (the "(?:" notation)) which I don't think are
allowed in XML Schema, but simple brackets should do.  IIRC it does
dates of the format YYYYMMDD, but it should be possible to re-jig it to
other formats.  If you think that's bad, have a look at what it takes to
do email addresses [1].

Getting back to the original question, my preferred solution would be to
use the standard XML Schema type and transform upon serialisation /
deserialisation, although I appreciate that this is not answering the
question asked.  This solution allows you to make use of the date
validation code built into schema validating parsers.

Paul
[1] http://www.ex-parrot.com//~pdw/Mail-RFC822-Address.html
-- 
Paul Warren, Client Services           DecisionSoft Limited
+44-1865-203192                        http://www.decisionsoft.com

Received on Thursday, 6 February 2003 05:49:44 UTC