Re: [xsl] Isolate single result from sorted list

Subject: Re: [xsl] Isolate single result from sorted list
From: "Eric Bréchemier" <eric.brechemier@xxxxxxxxx>
Date: Tue, 26 Jun 2007 01:25:47 +0200
Hi Mike,

On 6/25/07, Mike Wilt wrote:
(...) I can select and sort the start
times, but I am having trouble isolating the *next* start time.  In
other words, XSLT formats a list of results (as one would expect) but
I need to identify a single result.

I understand that you are using xsl:for-each with <xsl:sort data-type="number" select="substring(@start,9,4)"/> to sort your programme elements on the time part of the date contained in the @start attribute.

You would like to get the first programme matching the condition
start-time() > now() but you cannot stop the for-each after the first
element and you get:

The current time in seconds is: 64305
The next program starts at: 64500 | 64620 | 64800 | 65160 | 65400 |
65700 | 66000 | 66300 | 66600 | 66900 | 67500 | 68400 | 69180 | 69780
| 70200 | 70380 | 71700 | 72000 | 72300 | 72360 | 73500 | 73800 |
74100 | 75600 | 75660 | 75780 | 75900 | 77400 | 77460 | 78180 | 78300
| 78600 | 79200 | 79800 | 80100 | 80340 | 81000 | 82020 | 82800 |
83100 | 83160 | 83220 | 83700 | 83820 | 84300 | 84600 | 84900 | 85020
| 85200 | 85500 | 85800

when you would like to get
The current time in seconds is: 64305
The next program starts at: 64500


(...) I will be eternally grateful if someone could suggest a simple, straightforward solution.
Let's try ;)

I think the main issue is to remove all programme elements _before_
current time, because after that you can easily take the first
programme using
<xsl:if test="position()=1"> ... </xsl:if>

It seems that you could add a sort condition with the comparison expression:
  start-time() > $currentTime
where start-time() stands for
  60*( 60*number(substring(@start,9,2)) + number(substring(@start,11,2)) )

<xsl:sort data-type="number" select="60*(
60*number(substring(@start,9,2)) + number(substring(@start,11,2)) )
&gt; $currentTime" order="descending"/>
<xsl:sort data-type="number" select="substring(@start,9,4)"/>

with the above condition converted to 1 for true and 0 for false,
order="descending" puts true ahead of false.

Based on the two sort conditions, you get
- first programmes after currentTime, ordered by start time
- then programmes before currentTime, ordered by start time
and the first one is the one you are looking for (unless there are
none, in which case you have to check the first one is not actually
before $currentTime).

Kind Regards,

Eric Brichemier

Current Thread