Jump to content

XSLT 1.0: Looping through nodes


kwilliams

Recommended Posts

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 = Sunday1 = Monday2 = Tuesday3 = Wednesday4 = Thursday5 = Friday6 = SaturdayOk, 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:MondaySunday <!-- WRONGTuesdayWednesdayThursdayFridaySaturdayI 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):MondayTuesdayWednesdayThursdayFridaySaturdaySundayDoes anyone know how to do this? I'd appreciate your help.

Link to comment
Share on other sites

a little recursion did the trick for me,

			<xsl:template match="/">				<xsl:call-template name="doDays">					<xsl:with-param name="startDay">1</xsl:with-param>				</xsl:call-template>			</xsl:template>			<xsl:template name="doDays">				<xsl:param name="startDay"/>				<xsl:param name="count" select="1" />				<xsl:value-of select="//day[@id=$startDay]"/>					<xsl:if test="$count <= 7">						<xsl:call-template name="doDays">							<xsl:with-param name="startDay">								<xsl:choose>									<xsl:when test="$startDay = 7">0</xsl:when>									<xsl:otherwise><xsl:value-of select="$startDay + 1" /></xsl:otherwise>								</xsl:choose>							</xsl:with-param>							<xsl:with-param name="count" select="$count+1"/>						</xsl:call-template>					</xsl:if>			</xsl:template>

Link to comment
Share on other sites

a little recursion did the trick for me,
			<xsl:template match="/">				<xsl:call-template name="doDays">					<xsl:with-param name="startDay">1</xsl:with-param>				</xsl:call-template>			</xsl:template>			<xsl:template name="doDays">				<xsl:param name="startDay"/>				<xsl:param name="count" select="1" />				<xsl:value-of select="//day[@id=$startDay]"/>					<xsl:if test="$count <= 7">						<xsl:call-template name="doDays">							<xsl:with-param name="startDay">								<xsl:choose>									<xsl:when test="$startDay = 7">0</xsl:when>									<xsl:otherwise><xsl:value-of select="$startDay + 1" /></xsl:otherwise>								</xsl:choose>							</xsl:with-param>							<xsl:with-param name="count" select="$count+1"/>						</xsl:call-template>					</xsl:if>			</xsl:template>

Wow, that's great. 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!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...