Help with Recursion and Modulo together
This is an open discussion with 8 replies, filed under General.
Search
position()
gives you the info you seek. Perhaps something like this?
<xsl:template match="products/entry" mode="group"> <!-- I need to add classes one to four per iteration to this div --> <div class="group group-{position()}"> <xsl:apply-templates select=". | following-sibling::entry[position() < 4]" mode="product"/> </div> </xsl:template>
I recommend using group-1
, group-2
etc instead of words because words don't scale well whilst numbers do. However, if you're stuck with wordish classes, you can use a small template like this:
<xsl:template name="num-to-word"> <xsl:param name="num"/> <xsl:choose> <xsl:when test="$num = 1">one</xsl:when> <xsl:when test="$num = 2">two</xsl:when> <xsl:when test="$num = 3">three</xsl:when> <xsl:when test="$num = 4">four</xsl:when> ... </xsl:choose> </xsl:template>
Of course, silly me.
For some reason I forgot that the position()
is relative to the matched set, not (as I was thinking) the entire set.
Thanks very much for your insight there.
Apart from, it doesn't reset the count after 4 and just keeps counting.
I need to count one to four, one to four, one to four etc etc
I guess I'd need modulo again in the number template?
Answered my own question about the last little bit.
Thanks again!
You're welcome. Glad you figured it out.
Have I lately told you that I love XSLT? OK, it's rather verbose — but it's also a damned elegant concept.
+1, michael.
Create an account or sign in to comment.
I'll just set the stage:
I know how to use modulo to count every 4 products and apply a div wrapper to every 4, but what I can't wrap my head around is how to tell each div to have a class name of one, two, three, or four dependant on which iteration it is at, based on my layout groups.
Am I looking at this wrong? Should I be using just recursion here or have I got the right idea with modulo? How would I combine the two to achieve the following html
But with continuing over and over again.
The modulo stuff I have is:
Does this make sense what I need to achieve?