6 users online. Create an account or sign in to join them.Users
[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:
<articles> <entry id="1">...</entry> <entry id="2">...</entry> <entry id="3">...</entry> ... </articles>Desired output:
<div class="level-1"> <div class="level-2"> article-1 article-2 article-3 </div> <div class="level-2"> article-4 article-5 article-6 </div> </div> <div class="level-1"> <div class="level-2"> article-7 article-8 article-9 </div> ... </div> ...When trying to group for 2 levels:
<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[ position() mod 3 = 1 and position() < 6 ]" mode="level-2" /> </div> </xsl:template> <xsl:template match="entry" mode="lista-ablume-foto_group"> <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>I'm losing the first element in selection:
<div class="level-1"> <div class="level-2"> article-2 article-3 article-4 </div> <div class="level-2"> article-4 ... </div> </div> ...If I change this
to this (to add the first element)
I'm getting loads of duplicates ...
Any ideas? Thank you.