TR: how to check date strings

<[email protected]> Fri, 25 Apr 2003 11:56:21 +0200
Newsgroups gmane.text.xml.schematron
Message-ID <[email protected]>
Hi,

Thanks for your help.
This works fine.

However I still have a problem in one case: the schematron always check the date and sometimes the date is not necessary. But if you don't put a date there is an error!

Is it possible to define that when the date field is empty you don't check the date?
Or maybe could I check if a checkbox is true to verify the date?

Thanks
Sylvain

-----Message d'origine-----
De: Thomas G. Habing [mailto:[email protected]]
Date: lundi, 14. avril 2003 18:10
À: Thévoz Sylvain, IT-DTS-ENG-SDM-DAR
Cc: [email protected]
Objet: Re: [Schematron-love-in] how to check date strings


[email protected] wrote:
> Hello,
> 
> I would like to check that a string has this format: dd/mm/yyyy
> 
> Is there also a way to check that dd is between 1 and 31, mm is between 1 and 12?
> 
> 
> Thanks for any help
> Sylvain
> 
> 
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Schematron-love-in mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/schematron-love-in
> 
> 
Following are some tests that I use for validating dates for the YYYY-MM-DD 
format.  It should be fairly easy to modify this for use with the dd/mm/yyyy 
format.  Of course, use at your own risk...

<!-- CHECK FOR VALID YYYY-MM-DD -->
<sch:assert test="
   string-length(normalize-space(.))=10
   and string(number(substring(normalize-space(.),1,4)))!='NaN'
   and string-length(substring(normalize-space(.),1,4))=4
   and substring(normalize-space(.),5,1)='-'
   and string(number(substring(normalize-space(.),6,2)))!='NaN'
   and string-length(substring(normalize-space(.),6,2))=2
   and number(substring(normalize-space(.),6,2))>=1
   and number(substring(normalize-space(.),6,2))&lt;=12
   and substring(normalize-space(.),8,1)='-'
   and string(number(substring(normalize-space(.),9,2)))!='NaN'
   and string-length(substring(normalize-space(.),9,2))=2
   and number(substring(normalize-space(.),9,2))>=1
   and number(substring(normalize-space(.),9,2))&lt;=31
   ">
   <sch:name/> does not contain a valid W3CDTF (YYYY-MM-DD).
</sch:assert>

<!-- CHECK THAT FEBRUARY DOESN'T HAVE TOO MANY DAYS FOR LEAP YEARS -->
<sch:report test="(
   (
   number(substring(normalize-space(.),1,4)) mod 4 = 0
   and number(substring(normalize-space(.),1,4)) mod 100 != 0
   )
   or number(substring(normalize-space(.),1,4)) mod 400 = 0
   )
   and number(substring(normalize-space(.),6,2)) = 2
   and number(substring(normalize-space(.),9,2)) > 29">
  <sch:name/> does not contain a valid W3CDTF (February has a maximum of 29
  days in a leap year).
</sch:report>

<!-- CHECK THAT FEBRUARY DOESN'T HAVE TOO MANY DAYS FOR NON-LEAP YEARS -->
<sch:report test="not(
   (
   number(substring(normalize-space(.),1,4)) mod 4 = 0
   and number(substring(normalize-space(.),1,4)) mod 100 != 0
   )
   or number(substring(normalize-space(.),1,4)) mod 400 = 0
   )
   and number(substring(normalize-space(.),6,2)) = 2
   and number(substring(normalize-space(.),9,2)) > 28">
   <sch:name/> does not contain a valid W3CDTF (February has a maximum of 28
   days in a non-leap year).
</sch:report>

<!-- CHECK THAT APRIL DOESN'T HAVE TOO MANY DAYS -->
<sch:report test="number(substring(normalize-space(.),6,2))=4 and 
number(substring(normalize-space(.),9,2))>30">
   <sch:name/> does not contain a valid W3CDTF (April has a maximum of 30
   days).
</sch:report>

<!-- CHECK THAT JUNE DOESN'T HAVE TOO MANY DAYS -->
<sch:report test="number(substring(normalize-space(.),6,2))=6 and 
number(substring(normalize-space(.),9,2))>30">
   <sch:name/> does not contain a valid W3CDTF (June has a maximum of 30
   days).
</sch:report>

<!-- CHECK THAT SEPTEMBER DOESN'T HAVE TOO MANY DAYS -->
<sch:report test="number(substring(normalize-space(.),6,2))=9 and 
number(substring(normalize-space(.),9,2))>30">
   <sch:name/> does not contain a valid W3CDTF (September has a maximum of 30
   days).
</sch:report>

<!-- CHECK THAT NOVEMBER DOESN'T HAVE TOO MANY DAYS -->
<sch:report test="number(substring(normalize-space(.),6,2))=11 and 
number(substring(normalize-space(.),9,2))>30">
   <sch:name/> does not contain a valid W3CDTF (November has a maximum of 30
   days).
</sch:report>


-- 
Thomas Habing
Research Programmer, Digital Library Projects
University of Illinois at Urbana-Champaign
155 Grainger Engineering Library Information Center, MC-274
[email protected], (217) 244-4425
http://dli.grainger.uiuc.edu



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf