I have a feeling I'm approaching this in a very non-xslt way. Basically I have the code below, which works okay, but rather than all records being contained within a div with a class of "image-group", I want every group of 4 records to be within a div with a class of "image-group".
I've tried dynamically inserting the open and close div tags within the loop, but it's erroring because of unclosed tags. So I'm thinking this perhaps lends itself better to something like XSLT templates, but I'm not sure where to start.
Is this the right approach? Can someone nudge me in the right direction?
Basically, you will want to select every 4th item, and for each of them, render itself and the following 3 siblings. This can be done both with templates or nested for-each loops.
Thanks Morten. I've had a look at that reference and have also done a bit of 'group by postion' research. However, when I apply this to my scenario above, I just get the feeling I'm not doing it right. The following is kind of working in that it's grouping things into divs (just pulling the id of a media field out to test with) but it's the next part I'm struggling with; do I need to duplicate the bulk of the code in the middle to output the images 4 times (one for $nodes[1], again for $nodes[2] etc)? Seems like it should be more elegant than that?
It's for an event cycle. I need 4 events under the same div to make my jquery cycle happy.
Actually I have made my xpathnodeiterator in c# but couldn't figure out how to sort the list. But the best approach must be to have the sorted input before doing more with xslt.
The IF tests if the position is a modulus of 4 (and not the last node in the node-set) - then it would output the closing DIV and open a new DIV tag (with the 'image-group' class). I know it's not pure XSLT ... but it generates the required output HTML, and saves you any headache! ;-)
Yes, that would probably work, but where's the fun in that? ;-)
It would loose you a bit of the flexibility, like if you for example decided to put a dynamic css class on some of the elements or so. So I tend to avoid the CDATA method, because it creates elements that the XSLT is not aware of.
Add wrapper div conditionally
Hi,
I have a feeling I'm approaching this in a very non-xslt way. Basically I have the code below, which works okay, but rather than all records being contained within a div with a class of "image-group", I want every group of 4 records to be within a div with a class of "image-group".
The code I have is:
I've tried dynamically inserting the open and close div tags within the loop, but it's erroring because of unclosed tags. So I'm thinking this perhaps lends itself better to something like XSLT templates, but I'm not sure where to start.
Is this the right approach? Can someone nudge me in the right direction?
Thanks all.
Grouping is a classic challenge. This page has a couple of links on how to group by position:
http://www.jenitennison.com/xslt/grouping/
Basically, you will want to select every 4th item, and for each of them, render itself and the following 3 siblings. This can be done both with templates or nested for-each loops.
Let me know if this is total jibberish?
Thanks Morten. I've had a look at that reference and have also done a bit of 'group by postion' research. However, when I apply this to my scenario above, I just get the feeling I'm not doing it right. The following is kind of working in that it's grouping things into divs (just pulling the id of a media field out to test with) but it's the next part I'm struggling with; do I need to duplicate the bulk of the code in the middle to output the images 4 times (one for $nodes[1], again for $nodes[2] etc)? Seems like it should be more elegant than that?
If you do something like this, then you can write you layout for the nodes in the renderNode template:
Hi - Do you know if it's possible to sort the nodes before grouping them ?
Let's say I want to group by a date.
Not sure this approach is the one to go with, since this just groups x items together.
Is it for something like a news archive grouped by year/month or something like that?
Hi Morten,
It's for an event cycle. I need 4 events under the same div to make my jquery cycle happy.
Actually I have made my xpathnodeiterator in c# but couldn't figure out how to sort the list. But the best approach must be to have the sorted input before doing more with xslt.
Ahh, so getting events in groups of 4, sorted by date. Will give it a spin....
Are you using the new or the old xml schema?
Morten, that will be great
Yes I need a group by 4 and sorted by date. Actually I'm using my own xslt lib - so my foreach loop looks like this.
my xml look like
Hmm, I had to "cheat" a bit, by doing it in two steps. First saving the sorted events, and then doing the grouping, But it works :-)
Thank you Morten - it sure works.
I'll buy you a beer next time - Cheers :-)
Going back to Dan's original code snippet. Would this not work?
The IF tests if the position is a modulus of 4 (and not the last node in the node-set) - then it would output the closing DIV and open a new DIV tag (with the 'image-group' class). I know it's not pure XSLT ... but it generates the required output HTML, and saves you any headache! ;-)
Cheers, Lee.
Yes, that would probably work, but where's the fun in that? ;-)
It would loose you a bit of the flexibility, like if you for example decided to put a dynamic css class on some of the elements or so. So I tend to avoid the CDATA method, because it creates elements that the XSLT is not aware of.
Absolutely, I do agree! Sometimes there is a fine line between the "proper way" and "getting the job done" (quality vs convenience).
is working on a reply...