7

Using XSD i want to only accept date of the format YYYYMMDD in my xml field .. So how can i do that

I saw this in an example will this work ??

Praneel PIDIKITI
  • 17,359
  • 13
  • 39
  • 60
  • 6
    xs:date will be of the form YYYY-MM-DD. Why do you care what format the date is, in an xml? your app should take care of converting this to the required format. – asgs Feb 18 '11 at 09:43

1 Answers1

10

XML schema defines dateTime as ISO8601 with some exceptions and you should stick with this, otherwise you will get serious interoperability issues. If you want to send/receive date using different format, use simpleType with regular expression restriction and parse/format the date in your application code:

<xs:simpleType name="CustomDate">
    <xs:restriction base="xs:string">
        <xs:pattern value="\d{8}"/>
    </xs:restriction>
</xs:simpleType>

If you really want to mess around with built-in types (highly inadvisable), your XML framework/library might have some support for that. For instance in Java/JAXB you can apply custom transformers/formatters to any type, so that in client/server code you are still using Date object (not the 8-digit String), but it is marshalled/unmarshalled using your custom routine.

ulab
  • 1,039
  • 2
  • 15
  • 42
Tomasz Nurkiewicz
  • 324,247
  • 67
  • 682
  • 662