[Solved] Grouping items 2 levels deep
This is an open discussion with 3 replies, filed under XSLT.
Search
The following XSLT should do the trick:
<xsl:apply-templates select="articles/entry[position() mod 6 = 1]" mode="level-1" /> <xsl:template match="articles/entry" mode="level-1"> <div class="level-1"> <xsl:apply-templates select=". | following-sibling::entry[3]" mode="level-2" /> </div> </xsl:template> <xsl:template match="entry" mode="level-2"> <div class="level-2"> <xsl:apply-templates select=". | following-sibling::entry[position() < 3]" mode="article" /> </div> </xsl:template> <xsl:template match="entry" mode="article"> article-<xsl:value-of select="@id" /> </xsl:template>
Ha ! Nice one :D Noted down ... Thanks a lot !
I noticed this trick works for a 2*div.level-2
. But what if I need 5*div.level-2
inside div.level-1
?
<div class="level-1"> <div class="level-2"> article-1 ... </div> <div class="level-2"> article-6 ... </div> <div class="level-2"> article-11 ... </div> ... </div>
This won't work:
<xsl:apply-templates select=". | following-sibling::entry[ position() mod 3 = 1 ]" mode="level-2" />
Update
Got it working with
<xsl:apply-templates select=". | following-sibling::entry[ position() mod 3 = 0 and position() < 6 ]" mode="level-2" />
Understood it now. Well, at least I learned to use the FOR iterator :)
Create an account or sign in to comment.
Grouping items for one level deep is easy. But 2 levels, using matched templates? I only managed with loop iterator.
XML:
Desired output:
When trying to group for 2 levels:
I'm losing the first element in selection:
If I change this
to this (to add the first element)
I'm getting loads of duplicates ...
Any ideas? Thank you.