XSD Date Format

I am defining XSD. I need to define an element that accepts a date in yyyymmdd format. How to define a restriction in XSD just for accepting this format?

+6
xml xsd
source share
2 answers

You can always define it as a limited simple string-based type limited by a regular expression:

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

If you want to become really smart, you can configure the regular expression to be even more suitable for the date (for example, it contains information that the month can only be 01-12, etc.):

 <xs:simpleType name="FormattedDateType"> <xs:restriction base="xs:string"> <xs:pattern value="\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])"/> </xs:restriction> </xs:simpleType> 

Mark

+9
source share

If you need MM / DD / YYYY format in xml, this code can help you in this format

 <xs:element name="StartDate"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="\d{2}[/]\d{2}[/]\d{4}"/> <xs:length value="10"/> </xs:restriction> </xs:simpleType> </xs:element> 
-2
source share

All Articles