Click to See Complete Forum and Search --> : XSLT 1.0: Looping through nodes


kwilliams
12-01-2008, 04:12 PM
I need to loop through a set of nodes that contains the seven days of the week, like this:
<dayofweek>
<day id="0">Sunday</day>
<day id="1">Monday</day>
<day id="2">Tuesday</day>
<day id="3">Wednesday</day>
<day id="4">Thursday</day>
<day id="5">Friday</day>
<day id="6">Saturday</day>
</dayofweek>

The "id" values match up with the current weekday value from VB.NET using the DateTime.DayOfWeek method, like this:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday

Ok, now I want to loop through the seven days of the week, starting with the current day, which is Monday (1) for this example. So here's what I've tried so far:

<xsl:param name="current_weekday" select="1" /><!-- Monday -->
<xsl:for-each select="$this_page/dayofweek/day">
<xsl:sort order="descending" data-type="text" select="@id = $current_weekday" />
<xsl:value-of select="." /><br />
</xsl:for-each>

OUTPUT:

Monday
Sunday <!-- WRONG
Tuesday
Wednesday
Thursday
Friday
Saturday

I believe that this is because the sort is displaying the date that matches "1", but after that it sorts in the usual way. I cannot using proceeding-sibling and following-sibling, because I want it to look around to the beginning nodes on its own.

This is the output that I'd want if today was Monday (1):
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Does anyone know how to do this? I'd appreciate your help.

kwilliams
12-01-2008, 05:30 PM
I just got another solution from Michael Kay, and here it is for future note:
<xsl:for-each select="$this_page/dayofweek/day">
<xsl:sort order="ascending" data-type="number" select="(@id - $current_weekday + 7) mod 7" />
<td><xsl:value-of select="." /></td>
</xsl:for-each>

Thanks SO much for your help Michael!