Copied to clipboard

Flag this post as spam?

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


  • Mike 56 posts 88 karma points
    Jul 29, 2011 @ 22:32
    Mike
    0

    Grouping Articles by Year

    Hi all, I'm attempting to write some XSLT that will produce something similar to the following:

    2011

    • article 3 link
    • article 2 link

    2010

    • article 1 link

    Basically, I want the articles in chronological order, but grouped by year.

    Here's all I've gotten so far:

    <?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"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


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

    <xsl:param name="currentPage"/>
    <xsl:variable name="parentNode" select="1410"/>

        <xsl:template match="/">
          <h2>News Archive</h2>
            <xsl:for-each select="umbraco.library:GetXmlNodeById($parentNode)/newsArticle [@isDoc]">
              <xsl:sort select="date" order="descending"/>
       
                <xsl:if test="position() = 1">
                  <xsl:value-of select="umbraco.library:FormatDateTime(date, 'yyyy')"/>
                </xsl:if>

                <div class="newsArticle">
                  <span class="title"><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="title"/></a></span>
                  <span class="date"><xsl:value-of select="umbraco.library:FormatDateTime(date, 'MMMM d, yyyy')"/></span>
                </div>
              
           </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>

    You can see that I am grabbing the latest year, but I'm not sure how to grab the older years given the constraints of XSLT. Any input would be helpful, thanks!

     

     

  • Ernst Utvik 123 posts 235 karma points
    Jul 30, 2011 @ 00:54
    Ernst Utvik
    0

    Hi Mike

    See if you cant get some sense out of this: http://www.jenitennison.com/xslt/grouping/muenchian.html

    Another method would be to call a second template to do the year logic based on some passed parameters.

    Let us know how you do :)

  • Mike 56 posts 88 karma points
    Jul 30, 2011 @ 22:13
    Mike
    0

    For whomever stumbles upon this at a future date, I've seemingly had success with the "Grouping with xsl variable" outlined here: http://docstore.mik.ua/orelly/xml/xslt/ch06_02.htm

    I had actually encountered the same problem with the "Our First Attempt" mentioned in the article, which it did a good job of explaining. If you're working with several nodes, I'd recommend you follow the suggestion of the article and try for the Muench method (similar to what Ernst Utvik posted above - thanks Ernst).

    Anyways, here's my XSLT:

    <?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"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


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

    <xsl:param name="currentPage"/>
    <xsl:variable name="parentNode" select="1410"/>

        <xsl:template match="/">
          <h2>News Archive</h2>
            <xsl:for-each select="umbraco.library:GetXmlNodeById($parentNode)/newsArticle [@isDoc]">
              <xsl:sort select="date" order="descending"/>
                <xsl:variable name="previousYear" select="umbraco.library:FormatDateTime(date, 'yyyy')"/>
                <xsl:if test="not(preceding-sibling::newsArticle[umbraco.library:FormatDateTime(date, 'yyyy')=$previousYear])">
                  <xsl:value-of select="umbraco.library:FormatDateTime(date, 'yyyy')"/>
                  <xsl:for-each select="umbraco.library:GetXmlNodeById($parentNode)/newsArticle[umbraco.library:FormatDateTime(date, 'yyyy')=$previousYear]">
                    <xsl:sort select="date" order="descending"/>
                    <div class="newsArticle">
                      <span class="title"><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="title"/></a></span>
                      <span class="date"><xsl:value-of select="umbraco.library:FormatDateTime(date, 'MMMM d, yyyy')"/></span>
                    </div>
                  </xsl:for-each>
                </xsl:if>
           </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
Please Sign in or register to post replies

Write your reply to:

Draft