Copied to clipboard

Flag this post as spam?

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


  • trfletch 598 posts 604 karma points
    Apr 08, 2010 @ 17:07
    trfletch
    0

    List news items by the folder they are in

    Hi,

    I have an Umbraco V4.0.3 website and within it I have a news section that is using Autofolders 2.2 to create the following structure based on a date picker document property called weekEnding:

    News Section
       - 2010
             - April
                    -02
                          - Article 1
                          - Article 2
             - March
                     -29
                          - Article 3
                          - Article 4

    On my news page I want the news items to be displayed as follows but I am struggling to get started with the XSLT, I have created lots of news lists in the past using XSLT but nothing like this so wondered if someone could point me in the right direction. Many thanks. I want it to look as follows:

     

    News

    Week ending 2nd April 2010

    Article 1

    Article 2

     

    Week ending 29th March 2010

    Article 3

    Article 4

  • dandrayne 1138 posts 2262 karma points
    Apr 08, 2010 @ 17:34
    dandrayne
    0

    Hi again

    This xslt is a start, without solving your whole problem (that's the point!)

    It breaks up month and years using datefolders.  Your only challenge left would be to get the day from the final level of datefolders and print that,

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:autofolders.library="urn:autofolders.library"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets autofolders.library ">


    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- Anchor Nav -->
    <p>
    <xsl:for-each select="$currentPage/node [@nodeTypeAlias='DateFolder']">
    <xsl:sort select="@nodeName" data-type="number" order="descending" />
    <a href="#n{@nodeName}"><xsl:value-of select="@nodeName" /></a><xsl:if test="position() != last()">&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</xsl:if>
    </xsl:for-each>
    </p><br />



    <!-- For each year -->
    <xsl:for-each select="$currentPage/node [@nodeTypeAlias='DateFolder']">
    <xsl:sort select="@nodeName" data-type="number" order="descending" />
    <a name="n{@nodeName}" id="n{@nodeName}"><xsl:text> </xsl:text></a>
    <h2><img src="/css/images/newsbox20.gif" alt=" " />&nbsp;<xsl:value-of select="@nodeName" /></h2>

    <xsl:if test="count(current()/node) &gt; 0">
    <!-- For each month -->
    <xsl:for-each select="current()/node [@nodeTypeAlias='DateFolder']">
    <xsl:sort select="@nodeName" data-type="number" order="ascending" />


    <h4>&nbsp;
    <xsl:call-template name="MonthName">
    <xsl:with-param name="Month" select="@nodeName" />
    </xsl:call-template>&nbsp;
    <xsl:value-of select="../@nodeName" />
    </h4>

    <xsl:if test="count(current()/node [@nodeTypeAlias='NewsItem']) &gt; 0">
    <ul>
    <!-- For each news item -->
    <xsl:for-each select="current()/node [@nodeTypeAlias='NewsItem']">
    <li class="pxpad3">
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl(../@id)" />#<xsl:value-of select="position()"/>
    </xsl:attribute>
    <xsl:value-of select="current()/data[@alias='pageHeading']" />
    </a>
    </li>
    </xsl:for-each>
    </ul>
    <div class="top"><a href="#top">top</a></div>
    </xsl:if>

    </xsl:for-each>
    </xsl:if>


    </xsl:for-each>

    </xsl:template>

    <xsl:template name="MonthName">
    <xsl:param name="Month" />
    <xsl:choose>
    <xsl:when test="$Month = '01'">January</xsl:when>
    <xsl:when test="$Month = '02'">February</xsl:when>
    <xsl:when test="$Month = '03'">March</xsl:when>
    <xsl:when test="$Month = '04'">April</xsl:when>
    <xsl:when test="$Month = '05'">May</xsl:when>
    <xsl:when test="$Month = '06'">June</xsl:when>
    <xsl:when test="$Month = '07'">July</xsl:when>
    <xsl:when test="$Month = '08'">August</xsl:when>
    <xsl:when test="$Month = '09'">September</xsl:when>
    <xsl:when test="$Month = '10'">October</xsl:when>
    <xsl:when test="$Month = '11'">November</xsl:when>
    <xsl:when test="$Month = '12'">December</xsl:when>
    <xsl:otherwise>invalid month</xsl:otherwise>
    </xsl:choose>
    </xsl:template>


    </xsl:stylesheet>

    Hope this helps,
    Dan

  • trfletch 598 posts 604 karma points
    Apr 08, 2010 @ 17:57
    trfletch
    0

    Hi Dan,

    Thanks for the response I will take a look at that (although I am having a bit of an issue with Autofolders at the moment but that's another post!). I was reading the following article which I thought was quite interesting http://blackpoint.dk/umbraco-workbench.aspx?Snippet=/umbraco-workbench/xslt/grouping--distinct-values.aspx and wondered if maybe it would do the job.

  • trfletch 598 posts 604 karma points
    Apr 09, 2010 @ 15:46
    trfletch
    0

    Just to update for anyone who has a similar issue (I like to think I sometimes give back to the Umbraco community as well as pestering everyone with questions!), I have made this work with the following XSLT:

    <xsl:param name="currentPage"/> 
    <!-- This is the key definitions using the year and week ending (day) of the the node weekEnding property 
      - all nodes with the same week ending date get same week-key id, all nodes with the same year get the same year-key id -->
    <xsl:key name="years" match="node" use="umbraco.library:FormatDateTime(data[@alias='weekEnding'], 'yyyy')"/>
    <xsl:key name="week" match="node" use="umbraco.library:FormatDateTime(data[@alias='weekEnding'], 'yyyy-MM-dd')"/>
    <xsl:template match="/"> 
    <ul> <!-- Iterate over all nodes with a year-id similar to the first in every group -->
      <xsl:for-each select="$currentPage//node [ (generate-id() = generate-id(key('years', Exslt.ExsltDatesAndTimes:year(data[@alias='weekEnding']))[1]))]">
        <xsl:sort select="data[@alias='weekEnding']" order="descending" />
         <li>
        <ul>
          <xsl:variable name="week" select="key('years', Exslt.ExsltDatesAndTimes:year(data[@alias='weekEnding']))" />
          <!-- Iterate over all nodes with a week-id similar to the first in every group -->
          <xsl:for-each select="$week[generate-id() = generate-id(key('week',umbraco.library:FormatDateTime(data[@alias='weekEnding'], 'yyyy-MM-dd'))[1])]">
            <xsl:sort select="data[@alias='weekEnding']" order="descending" />
            <li><h2>Week ending <xsl:value-of select="umbraco.library:FormatDateTime(data[@alias='weekEnding'], 'dd MMMM yyyy')"/></h2>
              <ul> <!-- Iterate over all nodes in a group, i.e. with the same week-key -->
                <xsl:for-each select="key('week',umbraco.library:FormatDateTime(data[@alias='weekEnding'], 'yyyy-MM-dd'))">
                  <xsl:sort select="data[@alias='weekEnding']" order="descending" />
                  <li><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="data [@alias = 'articletitle']" /></a> </li>
                </xsl:for-each>
              </ul>
            </li>
          </xsl:for-each>
        </ul>
        </li>
      </xsl:for-each>
    </ul>
    </xsl:template>
Please Sign in or register to post replies

Write your reply to:

Draft