Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2008, 05:11 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT 1.0: Looping through nodes

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" />
<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
 
Old December 1st, 2008, 05:33 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try sorting on ((@id - $current_weekday) mod 7)

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old December 1st, 2008, 05:43 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

It's nice to hear from you:)

I tried your suggestion, like this:
<xsl:sort order="ascending" data-type="text" select="((@id - $current_weekday) mod 7)" />

And this was the result:
Monday
Tuesday
Sunday
Wednesday
Thursday
Friday
Saturday

So it changed a bit, but Sunday is still out of place. Any other suggestions would be great. Thanks!

KWilliams
 
Old December 1st, 2008, 06:16 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I forgot that -2 mod 7 is not 5 but -2. So you need this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="first"/>
<xsl:output indent="yes"/>

<xsl:template match="/">
<xsl:for-each select="/*/day">
  <xsl:sort select="(@id - $first + 7) mod 7" data-type="number"/>
  <xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

I can't see how you're getting Sunday in the wrong place. The above works for me.


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old December 1st, 2008, 06:21 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

IT WORKED GREAT!!!

I think that the reason Sunday was out of place was that in messing around with the code, I had changed the data-type from "number" to "text". Once I changed that back and added in the new sort method, it worked great. Thanks so much Michael. You really rock. Have a good one!

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT for Variable Child Nodes jlagedo XSLT 2 September 10th, 2006 03:00 AM
Removing nodes with an XSLT Jza XSLT 2 April 19th, 2006 10:06 AM
XSLT Looping Logic - Very Urgent ujayaraman XSLT 2 March 3rd, 2005 10:42 AM
xslt looping richjo100 XSLT 6 September 23rd, 2004 11:20 AM
xslt looping with tables... lukemedway_uk XSLT 16 December 24th, 2003 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.