mba12
mba12

Reputation: 2782

XML / XSD ISODateTime What is the correct format?

I am working with some old XML/XSD code that requires dates to be in 'ISODateTime' format. I have tried a number of different ISO date/time formats, i.e. with 'T' in the middle, but the validation continues to fail.

Am I missing something obvious? The XML and XSD snippets are below along with the error message.

XSD:

<xs:complexType name="MessageId">
    <xs:sequence>
        <xs:element name="Id" type="Max35Text"/>
        <xs:element name="Credit" type="ISODateTime"/>
    </xs:sequence>
</xs:complexType>

XML:

<ParentId>
    <Id>unique id</Id>
    <Credit>2019-09-27 04:00:00</Credit>
</ParentId>   <!-- Fixed by edit -->

Validation error:

ERROR: Element '{urn:iso:std:iso:20022:tech:xsd:tsmt.017.001.03}Credit': '2019-09-27 04:00:00' is not a valid value of the atomic type '{urn:iso:std:iso:20022:tech:xsd:tsmt.017.001.03}ISODateTime'.

Upvotes: 1

Views: 10665

Answers (3)

matthew
matthew

Reputation: 161

ISODateTime is defined at https://www.iso20022.org/standardsrepository/type/ISODateTime.

It is a simply a subtype of xsd:dateTime with the restrictions noted in the definition above. Note the extra definitions around things such as beginning and end of day, that are not present in xsd:dateTime. Note the requirement for an explicit time offset. These are subtle changes from xsd:dateTime.

The XML Schemas don't enforce this, which is why it works when you wrote a valid xsd:dateTime with a "T".

Upvotes: 2

Michael Kay
Michael Kay

Reputation: 163262

Judging from the error message, the type ISODateTime is in namespace urn:iso:std:iso:20022:tech:xsd:tsmt.017.001.03. ISO 20022 is a set of standards used by the financial industry. I'm not familiar with it, but I found a description of this data type at https://tools20022.com/apidocs/com/tools20022/repository/dict/datatype/ISODateTime.html

That page doesn't actually give an XSD definition of the type, and from the description I don't see any difference from the standard W3C type xs:dateTime. However, it would merit some further research into the actual schemas. It's possible that it might impose some restrictions from the W3C type, for example it could make the fractional seconds or timezone mandatory or restrict the range of years permitted.

However, the validation error that you've quoted is because you have a space rather than a "T" between the date and time parts of the value.

Upvotes: 2

zx485
zx485

Reputation: 29022

I could make your XSD work by changing your date format to xs:dateTime from the namespace http://www.w3.org/2001/XMLSchema, in your example this is 2019-09-27T04:00:00.

I did not find a definition for ISODateTime, but at W3.org is a definition for date/time formats and the closest I found was xs:dateTime described here.

So change your XML to (by simply adding the "T" you mentioned)

<?xml version="1.0" encoding="UTF-8"?>
<ParentId>
    <Id>unique id</Id>
    <Credit>2019-09-27T04:00:00</Credit>
</ParentId>

and this sample XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="MessageId">
        <xs:sequence>
            <xs:element name="Id" type="xs:string"/>   <!-- Changed for simplicity -->
            <xs:element name="Credit" type="xs:dateTime"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="ParentId" type="MessageId" />

</xs:schema>

will validate your XML.

Upvotes: 2

Related Questions