How to validate DD MM YYYY date with XML Schema?

时间:2016-02-03 03:36:13

标签: xml xsd xsd-validation

I am trying to validate date format 23 December 2012 date format with XML schema using type="xsd:date"

Cvc-datatype-valid.1.2.1: '23 December 2012' Is Not A Valid Value For 'date'.. Line '12', Column '53'.

Element example:

<dateReleased>23 December 1966</dateReleased>

Schema example:

<xsd:element name="dateReleased" type="xsd:date" />

Is it possible using type attribute (xs:date)? Or, do I need to use xs:pattern instead?

1 个答案:

答案 0 :(得分:1)

You cannot redefine the format accepted by xs:date in XSD.

You could use xs:pattern regex constraints to get close, however you won't be able to capture full date semantics (day ranges for months, leap years, etc).

If you have control over the date formats, go with the standard one supported by XSD. If you do not, consider transforming the XML via XSLT, which has more flexibility in defining date formats, ahead of validation.