I have lots of media files that are purposed to be downloaded. I want to archieve all the media files based on the year, then month and (in descending order).
I take that this should be done in XSLT? :-) If so, you should be able to use the <xsl:sort /> when looping through your collections. Have a look at this:
Currently I have 25 records on the $files, and sorted them according to year, month, date and then time. All of them are of year 2011 except one that is 2009.
So basically what I want is to make a group according to year. At the moment I couldn't divide the node-set according to year.
I tried to make it more usuable by modify the xslt. Now I would like to have something like
2011 (3)
--Jun (2)
--file1
--file2
--Feb (1)
--file3
2009 (1)
--Dec (1)
--file4
Basically I would like to have group by month from the nodeset that is group by year. I am getting node set grouped by year but then tried to group them further by month. but all the months are shown on both the year group.
Great - I was just about to post, but hit refresh first... :-)
Only real difference in my suggestion is to include the year in the FileByMonth key (e.g. use="substring(@createDate, 1, 7)" instead) but they both work.
Archieve media files according to creation date
Hi to All,
I have lots of media files that are purposed to be downloaded. I want to archieve all the media files based on the year, then month and (in descending order).
like
2010 (17)
--dec (5)
-- nov (12)
2009 (4)
--may (2)
--apr (2)
Hi praveity,
I take that this should be done in XSLT? :-) If so, you should be able to use the <xsl:sort /> when looping through your collections. Have a look at this:
http://our.umbraco.org/wiki/how-tos/xslt-useful-tips-and-snippets/conditional-xslt-sorting
All the best,
Bo
Hi Mortensen,,
I am already sorting the list. Here is my full xslt
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]> <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:ucomponents.cms="urn:ucomponents.cms" xmlns:ucomponents.dates="urn:ucomponents.dates" xmlns:ucomponents.io="urn:ucomponents.io" xmlns:ucomponents.members="urn:ucomponents.members" xmlns:ucomponents.strings="urn:ucomponents.strings" xmlns:ucomponents.urls="urn:ucomponents.urls" xmlns:ucomponents.xml="urn:ucomponents.xml" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" xmlns:TagCloudHelper="urn:TagCloudHelper" exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ucomponents.cms ucomponents.dates ucomponents.io ucomponents.members ucomponents.strings ucomponents.urls ucomponents.xml PS.XSLTsearch TagCloudHelper "> <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:param name="currentPage"/> <xsl:variable name="mediaFolder"> <xsl:choose> <xsl:when test="$currentPage/ancestor-or-self::*[@level = 1]/SC_LinkAndDownloads[@isDoc]/mediaContent != ''"> <xsl:value-of select="$currentPage/ancestor-or-self::*[@level = 1]/SC_LinkAndDownloads[@isDoc]/mediaContent"/> </xsl:when> <xsl:otherwise> <xsl:text>-1</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:template match="/"> <xsl:if test="$mediaFolder != ''"> <div class="pink_border_wrap"> <div class="pink_border_top_curve"> <img src="/images/mapholder_top_curve.jpg" /> </div> <div class="pink_border_mid_wrap" style="background:#ffffff;"> <div class="pink_border_mid_wrap_spacer"> <xsl:variable name="files" select="umbraco.library:GetMedia($mediaFolder ,1)//File"/> <xsl:for-each select="$files[substring(@createDate,1,4) = '2011']"> <xsl:sort select="umbraco.library:FormatDateTime(@createDate, 'yyyy')" order="descending" data-type="number"/> <xsl:sort select="umbraco.library:FormatDateTime(@createDate, 'MM')" order="descending" data-type="number"/> <xsl:sort select="umbraco.library:FormatDateTime(@createDate, 'dd')" order="descending" data-type="number"/> <xsl:sort select="umbraco.library:FormatDateTime(@createDate, 'HH')" order="descending" data-type="number"/> <xsl:sort select="umbraco.library:FormatDateTime(@createDate, 'mm')" order="descending" data-type="number"/> <xsl:sort select="umbraco.library:FormatDateTime(@createDate, 'ss')" order="descending" data-type="number"/> <xsl:variable name="lastCreateDate" select="substring(@createDate,1,4)"/> <xsl:value-of select="./title"/> <br/> </xsl:for-each> </div> </div> <div class="pink_border_footer_wrap"> <img src="/images/mapholder_bottom_curve.jpg" /> </div> </div> </xsl:if> </xsl:template> </xsl:stylesheet>Currently I have 25 records on the $files, and sorted them according to year, month, date and then time. All of them are of year 2011 except one that is 2009.
So basically what I want is to make a group according to year. At the moment I couldn't divide the node-set according to year.
e.g.
2011
--list all 2011 files [AND THEN]
2009
--list all 2009 files
Hope you got my idea :)
Hi praveity,
You'll need yourself some "grouping-fu" - here's a basic setup:
<!-- Index all File items by year --> <xsl:key name="FileByYear" match="File" use="substring(@createDate, 1, 4)" /> <xsl:template match="/"> <xsl:variable name="files" select="umbraco.library:GetMedia($mediaFolder, true())//File" /> <!-- Magic - google "Muenchian Grouping" if you're curious :-) --> <xsl:for-each select="$files/File[count(. | key('FileByYear', substring(@createDate, 1, 4))[1]) = 1]"> <xsl:sort select="@createDate" data-type="text" order="descending" /> <!-- Print header for this year --> <xsl:apply-templates select="." mode="header" /> <!-- Process the files in this year --> <xsl:apply-templates select="key('FileByYear', substring(@createDate, 1, 4))" mode="item"> <xsl:sort select="@createDate" data-type="text" order="descending" /> </xsl:apply-templates> </xsl:for-each> </xsl:template> <!-- Output for header --> <xsl:template match="File" mode="header"> <h2><xsl:value-of select="substring(@createDate, 1, 4)" /></h2> </xsl:template> <!-- Output for each item --> <xsl:template match="File" mode="item"> <xsl:value-of select="title" /> </xsl:template>One thing I'd like to mention though: All of the sort statements in your original code can be replaced by one single sort:
No need to call the FormatDateTime() extension multiple times - the XML date format was designed to be sortable as-is.
/Chriztian
Hi Chriztian,
Thanks for the xslt, you were correct about the sorting. I have the list of all files in descending order.
My complete xslt is as follows
<xsl:variable name="mediaFolder"> <xsl:choose> <xsl:when test="$currentPage/ancestor-or-self::*[@level = 1]/SC_LinkAndDownloads[@isDoc]/mediaContent != ''"> <xsl:value-of select="$currentPage/ancestor-or-self::*[@level = 1]/SC_LinkAndDownloads[@isDoc]/mediaContent"/> </xsl:when> <xsl:otherwise> <xsl:text>-1</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="files" select="umbraco.library:GetMedia($mediaFolder, true())//dFile" /> <xsl:key name="FileByYear" match="dFile" use="substring(@createDate, 1, 4)" /> <xsl:template match="/"> <xsl:if test="$mediaFolder != ''"> <!-- Magic - google "Muenchian Grouping" if you're curious :-) --> <xsl:for-each select="$files[count(. | key('FileByYear', substring(@createDate, 1, 4))[1]) = 1]"> <xsl:sort select="@createDate" data-type="text" order="descending" /> <!-- Print header for this year --> <xsl:apply-templates select="." mode="header" /> <ul> <!-- Process each item in this group --> <xsl:apply-templates select="key('FileByYear', substring(@createDate, 1, 4))" mode="item"> <xsl:sort select="@createDate" data-type="text" order="descending" /> </xsl:apply-templates> </ul> <br/> </xsl:for-each> </xsl:if> </xsl:template> <xsl:template match="dFile" mode="header"> <h2> <xsl:value-of select="substring(@createDate, 1, 4)" /> (<xsl:value-of select="count(key('FileByYear', substring(@createDate, 1, 4)))"/>) <!-- this is the count --> </h2> </xsl:template> <xsl:template match="dFile" mode="item"> <xsl:variable name="downloadFile" select="umbracoFile" /> <xsl:if test="linkText != '' "> <xsl:value-of select="firstText" disable-output-escaping="yes"/> <li> <a href="{$downloadFile}" target="_blank"> <xsl:value-of select="linkText" disable-output-escaping="yes"/> </a> </li> <xsl:value-of select="remainingText" disable-output-escaping="yes"/> </xsl:if> </xsl:template> </xsl:stylesheet>The result is as follows
2011 (3)
--file1
--file2
--file3
2009 (1)
--file4
I tried to make it more usuable by modify the xslt. Now I would like to have something like
2011 (3)
--Jun (2)
--file1
--file2
--Feb (1)
--file3
2009 (1)
--Dec (1)
--file4
Basically I would like to have group by month from the nodeset that is group by year. I am getting node set grouped by year but then tried to group them further by month. but all the months are shown on both the year group.
2011 (3)
May (1)
Jul (1)
Oct (1)
2009 (1)
May (1)
Jul (1)
Oct (1)
Finally, I succeeded in having the expected results. Here is the working xslt, hope this could help other umbraco developers.. cheers
<xsl:variable name="mediaFolder"> <xsl:choose> <xsl:when test="$currentPage/ancestor-or-self::*[@level = 1]/SC_LinkAndDownloads[@isDoc]/mediaContent != ''"> <xsl:value-of select="$currentPage/ancestor-or-self::*[@level = 1]/SC_LinkAndDownloads[@isDoc]/mediaContent"/> </xsl:when> <xsl:otherwise> <xsl:text>-1</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="files" select="umbraco.library:GetMedia($mediaFolder, true())//dFile" /> <xsl:key name="FileByYear" match="dFile" use="substring(@createDate, 1, 4)" /> <xsl:key name="FileByMonth" match="dFile" use="substring(@createDate, 6, 2)" /> <xsl:template match="/"> <xsl:if test="$mediaFolder != ''"> <!-- Magic - google "Muenchian Grouping" if you're curious :-) --> <xsl:for-each select="$files[count(. | key('FileByYear', substring(@createDate, 1, 4))[1]) = 1]"> <xsl:sort select="@createDate" data-type="text" order="descending" /> <!-- Print header for this year --> <div class="archivelist"> <xsl:apply-templates select="." mode="year" /> <xsl:variable name="prab" select="key('FileByYear', substring(@createDate, 1, 4))" /> <xsl:for-each select="$prab[count(. | key('FileByMonth', substring(@createDate, 6, 2))[1]) = 1]"> <xsl:sort select="@createDate" data-type="text" order="descending" /> <xsl:apply-templates select="." mode="month" /> <ul> <xsl:for-each select="key('FileByMonth', substring(@createDate, 6, 2))"> <xsl:apply-templates select="." mode="item" /> </xsl:for-each> </ul> </xsl:for-each> </div> </xsl:for-each> </xsl:if> </xsl:template> <xsl:template match="dFile" mode="year"> <h2 class="archivelist_header"> <xsl:value-of select="substring(@createDate, 1, 4)" /> (<xsl:value-of select="count(key('FileByYear', substring(@createDate, 1, 4)))"/>) </h2> </xsl:template> <xsl:template match="dFile" mode="month"> <h2> <xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'MMM')" /> (<xsl:value-of select="count(key('FileByMonth', substring(@createDate, 6, 2)))"/>) </h2> </div> </xsl:template> <xsl:template match="dFile" mode="item"> <xsl:variable name="downloadFile" select="umbracoFile" /> <xsl:if test="linkText != '' "> <li> <xsl:value-of select="firstText" disable-output-escaping="yes"/> <a href="{$downloadFile}" target="_blank"> <xsl:value-of select="linkText" disable-output-escaping="yes"/> </a> <xsl:value-of select="remainingText" disable-output-escaping="yes"/> </li> </xsl:if> </xsl:template> </xsl:stylesheet>Great - I was just about to post, but hit refresh first... :-)
Only real difference in my suggestion is to include the year in the FileByMonth key (e.g. use="substring(@createDate, 1, 7)" instead) but they both work.
/Chriztian
Thanks for your dedication and help.
I tried your suggestion as well yes it does work :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.