Discussion:
custom date type in xml schema
Hanna
2003-02-06 04:36:06 UTC
Permalink
Dear all,
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.
Please help.
xsd:
<simpleType name='mydatetype'>
<restriction base='date'>
<pattern value='MM-DD-CCYY'/ >
</restriction>
</simpleType>

xml:
<birthday>10-29-2002</birthday>
Regards,
Hanna
Jeni Tennison
2003-02-06 10:15:09 UTC
Permalink
Hi Hanna,
Post by 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/
Paul Warren
2003-02-06 10:49:40 UTC
Permalink
Post by Jeni Tennison
<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>
- 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
Bob Schloss
2003-02-06 18:57:02 UTC
Permalink
Hanna,

First, ask yourself why you care how the characters representing a
date are coded in an attribute or element in an XML instance document.
There are reasons to think you may regret doing this.

In the future, when improved XML Query tools are available, you may
regret that you didn't use the standard coding. It should not be hard,
whenever date is presented to a user, to convert to the coding most
familiar to your user community using facilities of XSLT (for example), or
tools that will present and retrieve XML data from users based on schema
types connected with the forthcoming W3C XForms specifications.

Secondly, should you decide that you have a compelling reason to have
your own coding, the pattern facet has to be defined using a regular
expression language which is described at:
http://www.w3.org/TR/xmlschema-0/#regexAppendix for an overview
http://www.w3.org/TR/xmlschema-2/#regexs for all the details
Therefore, you would code something like:
<pattern value="NN\-NN\-NNNN"/>

Good luck,
Bob Schloss

XML/XSL Transformational Systems
IBM Thomas J. Watson Research Center
Yorktown Heights, NY, USA






"Hanna"
<***@hotmail To: <xmlschema-***@w3.org>
.com> cc:
Sent by: Subject: custom date type in xml schema
xmlschema-dev-req
***@w3.org


02/05/2003 11:36
PM





Dear all,
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.
Please help.
xsd:
<simpleType name='mydatetype'>
<restriction base='date'>
<pattern value='MM-DD-CCYY'/ >
</restriction>
</simpleType>
xml:
<birthday>10-29-2002</birthday>
Regards,
Hanna

Loading...