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:dateTime
would 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 simpleType
definition 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:string
and 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With