Re: Date type and patterns

Hi Danny,

> I've been testing a schema that has an element defined as type
> xsd:date. Based upon our current specifciation we have a defintion
> of a date type defined to only have the year value required, month
> and day are optional and there should be no time information (we
> have a datetime type defined for this).

As you've noticed, your definition of a "date" type is different from
the XML Schema definition of a "date" type. In XML Schema, a date is a
year, month and day, with a lexical representation in the format
YYYY-MM-DD. XML Schema has a different data type (gYear) for years.
These are both primitive data types in XML Schema, which means that
their semantics and their syntax is fixed. You can't make up a new
syntax for a date and expect an XML Schema processor to recognise it
as a date.

What I think you should do is create your own 'date' type (in your own
namespace) that allows either xs:gYear or xs:date values, by creating
a union of these two types:

<xs:simpleType name="date">
  <xs:union memberTypes="xs:date xs:gYear" />
</xs:simpleType>

> After reading the Scheam spec it looks like the definition of the
> date type requires a year month and day and it allows this to be
> changed via a pattern like this:
>
>          <xsd:pattern value="\d{4}(\-\d{2}(\-\d{2})?)?"/>

When you restrict a data type using xs:pattern, what you're doing is
saying "accept any values that are of the base data type *and* whose
lexical representation meets this pattern". Changing the pattern
doesn't change the kind of lexical representations that a given data
type accepts, it can only restrict those that it can already accept.
If you do:

<xs:simpleType name="myDate">
  <xs:restriction base="xs:date">
    <xs:pattern value="\d{4}" />
  </xs:restriction>
</xs:simpleType>

then you're saying that values of type 'myDate' have to be XML Schema
dates *and* have to have a lexical representation of 4 digits. But
this is impossible, because according to XML Schema all xs:date values
have to be of the form YYYY-MM-DD. The string "2002" isn't a valid
representation of a xs:date and never can be. Processors can't really
pick up on this contradiction when you're creating them, but they
should reject any values that are supposed to be of this type.

Cheers,

Jeni

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

Received on Monday, 22 April 2002 08:26:28 UTC