Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1288 posts 3921 karma points c-trib
    Feb 08, 2011 @ 15:01
    Dan
    0

    Simple Muenchian grouping

    Hi,

    I have a list of journal articles on my site.  I'd like to show an archive of these, grouped by year, so my rendered HTML will be something like:

    <ul class="archive">
    <li><a href="/archive.aspx?year=2011">2011</a></li>
    <li><a href="/archive.aspx?year=2010">2010</a></li>
    </ul>

    I know I can do this by using a for-each and preceeding-sibling, but I'd like to try Muenchian grouping for a more elegant solution.  I've tried the following, but just can't get it to work:

    <xsl:key name="article-years" match="JournalArticle" use="articleDate" />
    <xsl:template match="/">
     <xsl:for-each select="key('article-years', articleDate)">
      <xsl:value-of select="articleDate" /><br />
    </xsl:for-each>
    </xsl:template>

    Where 'article years' is the name of the key, 'JournalArticle' is the document type alias and 'articleDate' is the data type alias of the date I want to group by.

    Can anyone suggest what I'm doing wrong?  I suspect it's to do with the 'match' field, but I've tried a few different things here to no avail.

    Thanks

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 08, 2011 @ 15:18
    Tom Fulton
    1

    Hi Dan,

    Is articleDate a datepicker?  If so I think you might need to add some Date Formatting in there to get the year.  Here's code from something similar I did that should get you started:

    <xsl:key name="year" match="* [@isDoc]" use="umbraco.library:FormatDateTime(sermonDate, 'yyyy')"/>

      <xsl:for-each select="$currentPage/descendant::* [generate-id() = generate-id(key('year', umbraco.library:FormatDateTime(./sermonDate,'yyyy'))[1])]">
          <xsl:sort select="umbraco.library:FormatDateTime(sermonDate, 'yyyy')" data-type="number" order="descending" />
          <xsl:value-of select="umbraco.library:FormatDateTime(./sermonDate,'yyyy')"/>
        </xsl:for-each>

    -Tom

  • Dan 1288 posts 3921 karma points c-trib
    Feb 08, 2011 @ 15:34
    Dan
    0

    Great, with a few mods that's sorted it!  Thanks Tom.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Feb 08, 2011 @ 19:05
    Chriztian Steinmeier
    3

    Hi Dan,

    Just to help you get through your initial attempt and provide a "Simple Muenchian Grouping" example (if such a thing exists :-) - 'coz you were off to a great start, actually...

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- Select the root "folder" of JournalArticle documents here -->
        <xsl:variable name="articleRoot" select="$siteRoot/Journals" />
    
        <!-- Creates index of all JournalArticles by year -->
        <xsl:key name="article-years" match="JournalArticle" use="substring(articleDate, 1, 4)" />
    
        <xsl:template match="/">
            <ul class="archive">
                <xsl:for-each select="$currentPage/JournalArticle[count(. | key('article-years', substring(articleDate, 1, 4))[1]) = 1]">
                    <xsl:sort select="articleDate" data-type="text" order="descending" />
                    <xsl:variable name="year" select="substring(articleDate, 1, 4)" />
    
                    <li>
                        <a href="{umb:NiceUrl(@id)}?year={$year}">
                            <xsl:value-of select="$year" />
                        </a>
                    </li>
    
                </xsl:for-each>
            </ul>
        </xsl:template>
    
    </xsl:stylesheet>
    

    /Chriztian 

  • Dan 1288 posts 3921 karma points c-trib
    Feb 09, 2011 @ 10:25
    Dan
    0

    A wonderful and very helpful reply, as always, thanks Chriztian.

  • Rob Watkins 369 posts 701 karma points
    Feb 23, 2011 @ 16:19
    Rob Watkins
    0

    Is there any reason why the Muenchian grouping examples always use the count() / union method and not the generate-id method?

    I have been trying to get my head round the whole thing and Jeni Tennison's article here offers both as options; however, the count() / union variation is almost invariably used in examples.

    Is it more efficient or is it just a style thing? I find the generate-id method more intuitive to read, but that's probably because I suck at set arithmetic :o)

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Feb 23, 2011 @ 21:28
    Chriztian Steinmeier
    0

    @Rob: I have no idea - haven't tried to benchmark it - but now I'm curious, so I'll probably do some testing. Doubt there's much difference, though...

    /Chriztian 

Please Sign in or register to post replies

Write your reply to:

Draft