Copied to clipboard

Flag this post as spam?

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


  • Inmedia 124 posts 176 karma points
    May 06, 2012 @ 23:22
    Inmedia
    0

    Group news by Years and Months

    I have a news archieve which is structured like this -

    - News
       - Year
         - Month
           - News Item

    My doc types are are named as shown above.

    What I am trying to do, is create an XSLT file that list the years and months so:

       2012
           - May
           - April
           - March
           - February
           - January
       2011
           - December
           - November
           - Oktober
           - September

    - Etc. etc.

    I am pretty sure this is a simple thing to achieve if you know your XSLT well, which I apparently don't... :)

    Can someone please help my out here?


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 06, 2012 @ 23:31
    Chriztian Steinmeier
    1

    Hi Inmedia,

    You can start with something like this - a template for everything you need to show:

    <?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]" />
    
        <!-- Find the News node here - we base everything off this, so needs to be right -->
        <xsl:variable name="newsRoot" select="$siteRoot/News" />
        <xsl:variable name="years" select="$newsRoot/Year" />
    
        <xsl:template match="/">
            <!-- Show all years that have at least one Month node -->
            <xsl:apply-templates select="$years[Month]">
                <xsl:sort select="@nodeName" data-type="number" order="descending" />
            </xsl:apply-templates>
        </xsl:template>
    
        <xsl:template match="Year">
            <h2><xsl:value-of select="@nodeName" /></h2>
            <ul>
                <xsl:apply-templates select="Month">
                    <xsl:sort select="@createDate" data-type="text" order="descending" />
                </xsl:apply-templates>
            </ul>
        </xsl:template>
    
        <xsl:template match="Month">
            <li><xsl:value-of select="@nodeName" /></li>
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Inmedia 124 posts 176 karma points
    May 06, 2012 @ 23:43
    Inmedia
    0

    Chriztian to the rescue... Again! :D

    Thank you so much - It works like a charm!  :) 
    Fantastic with response to my post THAT fast...

  • Inmedia 124 posts 176 karma points
    May 08, 2012 @ 21:31
    Inmedia
    0

    Turns out, it needs a little more tweeking, for it to work...

    I can list the correct elements with the XSLT you gave me, but when I try to add a NiceUrl link to the list items, I get an error.

    Can you help me put a link on every item, including the years?

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 13, 2012 @ 00:14
    Chriztian Steinmeier
    0

    Hi Inmedia,

    Sorry, I must have missed your 2nd reply - I think I know what's wrong (I could be totally wrong, though) - I have a habit of renaming the overly long prefix for the umbraco.library functions to just "umb" - but that of course requires you to know about how namespaces and prefixes work, because suddenly the umbraco.library:NiceUrl() doesn't exist... So here's what should work, in the code I posted above, e.g. for the "Years" node:

    <h2><a href="{umb:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a></h2>

    Hope that's what you were running into - otherwise, please reply again.

    /Chriztian 

     

Please Sign in or register to post replies

Write your reply to:

Draft