3

In the xml schema data type --> date allows date as yyyy-mm-dd format by default.

How can we modify it so that it accepts yyyy/mm/dd format style instead ?

aditya parikh
  • 595
  • 4
  • 11
  • 30

3 Answers3

5

You can't get the standard xs:date type to accept formats other than the standard format. What you can do is to define a string data type with a pattern constraint; but it won't have date semantics, for example it will accept 1999/02/29.

Saxon's XSD processor has an experimental extension, saxon:preprocess, which allows you to change the lexical representation of a data type. Not many people use it, of course, because they want their schemas to be interoperable; but it does exactly what you want so I thought I would mention it.

Another solution which is often overlooked, and which is perhaps more practical, is for your processing pipeline to do a transformation step before it does the validation step. This approach can be used to overcome many of the limitations of XML Schema, and it isn't widely enough known.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
5

You cant change the date type to a specific format but you can take it as a string and choose a specific format that you want.

For the format of date MM/DD/YYYY. the code snippet is given below.

<xs:element name="StartDate">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:pattern value="\d{2}[/]\d{2}[/]\d{4}"></xs:pattern>
            <xs:length value="10"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>
Ben T
  • 4,656
  • 3
  • 22
  • 22
Sunil
  • 482
  • 6
  • 12
0

you can check this regex posted by melwil. it worked for me !

((0[1-9]|[1-2]\d|3[01])-(0[13578]|1[02])|([0-2]\d|30)-(0[469]|11)|(0[1-9]|1\d|2[0-8])-02)-\d{4}|(0[1-9]|[12]\d)-02-(([02468][048]|[13579][26])00|(\d{2}([02468][48]|[2468][048]|[13579][26])))
slassh
  • 11
  • 1
  • 2
    Please explain your answer instead of just dumping code. – Robert Sep 18 '16 at 23:47
  • 1
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Sep 19 '16 at 14:02
  • Since it's not my answer i've just linked it with melwil answer ! http://stackoverflow.com/questions/16684737/what-is-the-regular-expression-for-dd-mm-yyyy-date-format-in-xml-regex i can't assume that i'm the one who wrote it. if you want i can explain how this regex work your are welcome. – slassh Sep 20 '16 at 16:48