Validate custom date and time in XML with XSD
Solution 1
Your date and time requirements are not proper restrictions of xs:date
and xs:time
. If they were, making these changes would work (but just not be as tight as you'd like):
<xs:element name="testdate" type="xs:date"/>
<xs:element name="testtime" type="xs:time"/>
So, to use your patterns, you have to base them on xs:string
. You also have to take into account that in XSD's xs:pattern
regular expressions, there is already an implicit ^
anchor at the beginning and $
anchor at the end of the pattern. Removing the extra ones you've added eliminates the problem you were having.
XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<!-- Root element -->
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="testdate" type="zsdate"/>
<xs:element name="testtime" type="zstime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Date format used to validate dates formatted like 01.01.2015 -->
<xs:simpleType name="zsdate">
<xs:restriction base="xs:string">
<xs:pattern value="(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).(19|20)\d\d"/>
</xs:restriction>
</xs:simpleType>
<!-- Time format used to validate times formatted like 11:55, 23:59 etc. -->
<xs:simpleType name="zstime">
<xs:restriction base="xs:string">
<xs:pattern value="(0[0-9]|[1][0-9]|2[1-3]):([0-5][1-9])"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Solution 2
Better like this, to validate HH:50:
<xs:pattern value="(0[0-9]|[1][0-9]|2[1-3]):([0-5][0-9])"/>
Philippe
Updated on June 29, 2022Comments
-
Philippe almost 2 years
I have to validate dates formatted like this 09.02.2015 (DD.MM.YYYY) and times 14:05 (HH:MM), but can't find a way to do this.
Here's an example:
XML:
<?xml version="1.0"?> <root> <testdate>09.02.2015</testdate> <testtime>14:05</testtime> </root>
XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema"> <!-- Root element --> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="testdate" type="zsdate"/> <xs:element name="testtime" type="zstime"/> </xs:sequence> </xs:complexType> </xs:element> <!-- Date format used to validate dates formatted like 01.01.2015 --> <xs:simpleType name="zsdate"> <xs:restriction base="xs:date"> <xs:pattern value="^(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).(19|20)\d\d$"/> </xs:restriction> </xs:simpleType> <!-- Time format used to validate times formatted like 11:55, 23:59 etc. --> <xs:simpleType name="zstime"> <xs:restriction base="xs:time"> <xs:pattern value="^(0[0-9]|[1][0-9]|2[1-3]):([0-5][1-9])$"/> </xs:restriction> </xs:simpleType> </xs:schema>
I've tried to validate the XML with the notepad++ XML plugin and with this XML validator.
Notepad++ output:
Validation of current file using XML schema:
ERROR: Element 'testdate': '09.02.2015' is not a valid value of the atomic type 'zsdate'.
ERROR: Element 'testtime': '14:05' is not a valid value of the atomic type 'zstime'.When testing the regular expression with Regular Expression Tester, it matches correctly.
I've tried changing the types fromxs:date
andxs:time
toxs:string
, but the validation result is the same.Any idea what I'm doing wrong?
-
Philippe about 9 yearsThanks! I wasn't aware of the implicit
^
and$