Re: Validating a duration

"Rick Jelliffe" <[email protected]> Mon, 28 Jul 2003 19:15:18 +1000
Newsgroups gmane.text.xml.schematron
Message-ID <[email protected]>
From: "Robert Leftwich" <[email protected]>

> I am having difficulty writing a Schematron validation for a duration. The 
> (simplified) XML is as follows:

I think there is probably a better way than this using a more functional style, 
but I think this should work (I haven't tested it). Basically, it is

  if (endtime is midnight)
     compare using fixed end-time time of 13*60 minutes
  else
     compare using end-time provided


<!-- Variables for conversion to common units (minutes) -->
<sch:let name="currentStartTimeSecs" 
    value="number(substring(start-time, 1,2))*60 + number(substring(start-time,4,2))"/>
<sch:let name="currentEndTimeSecs" 
    value="number(substring(end-time, 1,2))*60 + number(substring(end-time,4,2))"/>
<sch:let name="currentCalculatedDurationSecs" 
    value="$currentEndTimeSecs - $currentStartTimeSecs"/>
<sch:let name="currentCalculatedDurationSecsForMidnight" 
    value="13*60 - $currentStartTimeSecs"/>
<sch:let name="durationSecs" 
    value="(number(normalize-space(duration/hours))*60 
    + number(normalize-space(duration/minutes)))" />

<!-- Boolean for exceptional case -->
<sch:let name="ends-at-midnight" 
    value="end-time='00:00'" />

<!-- Tests for the two cases -->
<sch:let name="normal-case" 
    value= "$currentCalculatedDurationSecs = $durationSecs" />
<sch:let name="midnight-case" 
    value= "$currentCalculatedDurationSecsForMidnight =  $durationSecs" />

<!-- The actual assertion -->
<sch:assert test="
    ($ends-at-midnight  and $midnight-case ) or 
    (not ( $ends-at-midnight) and $normal-case)  "/>


I hope this is some help. I should check up with XSCL people: they
have good experience with figuring out what the best idioms for
<let> are to avoid all the clunkiness above.

Cheers
Rick Jelliffe

(By the way, people who have implemented Schematron
1.5 in XSLT should be aware that the <sch:let> should be
incredibly trivial to implement: all it really needs is namesapce
conversion to <xslt:let> IIRC. )




-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01