views:

288

answers:

2

I have a legacy system that produces XML with timestamps similar to ISO 8601, but with space used to separate date and time. E.g. 2009-12-31 00:00:00. I would like to define a schema that defines the possible range for these timestamps. xsd:dateTimewould be well suited for that purpose, however, it uses T as the separator between date and time. I cannot modify the legacy system to return timestamps using T as a separator.

Is there a way to have a simpleTypedefinition that derives from xsd:dateTime, but alters the separator or do I have to rely on a string with an appropriate pattern and human readable comments?

Update: As I understand, I can use a pattern for dateTime to restrict the range of dateTime objects for input, but this does not alter the separator character.

Example:

<xs:restriction base="xs:dateTime">
  <xs:pattern value="[2].*:[0-9]{2}"/>
</xs:restriction>

This would only allow for dateTime with years starting with 2000 and without fractional seconds and time zone information.

Summary of answers:

It is not possible to use xs:dateTime as the base type for this. It is however possible to use xs:stringand define a pattern.

<xs:restriction base="xs:string">
  <xs:pattern value="[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]"/>
</xs:restriction>

By using a string, of course, any automatic tools that use the Schema to create language bindings will also retrieve a string, conversion to the appropriate language type for date/time has to be done by hand.

+2  A: 

I don't think you can derive something else from xsd:dateTime, as the XML processors will not be able to understand this. Your best bet is to use a string with the right pattern.

Francis Upton
yes .. I agree .. too
infant programmer
+4  A: 

You can do a regular expression pattern restriction on the xs:string data type.

For example:

<xs:element name="specialDateTime">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][9-9]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Edit

I found this example at http://www.cs.wisc.edu/condor/classad/refman/node9.html. It looks like you can put a pattern restriction on a dateTime:

<xsd:simpleType>
  <xsd:restriction base="xsd:dateTime">
    <xsd:pattern value="\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d[+\-]\d\d:\d\d" />
  </xsd:restriction>
</xsd:simpleType>

Hope that helps.

DanM
Yes I know. But I was hoping for something that derives from dateTime, so that I can use XMLBeans as an automated Schema-to-Java mapper.
nd
I've never tried using a `pattern` element with anything other than a string before, but maybe it would actually work with `dateTime`. It's certainly worth a try.
DanM
I can't think a better answer than this .. Certainly XSD bound with hell of restrictions .. You can't use XSD in your own way ..
infant programmer