Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    May 18, 2010 @ 14:41
    Gordon Saxby
    0

    Create a directory list of media items

    I feel sure this should be easy, but I seem to have been going round in cirlces this morning!!

    I am trying to create a XSLT that will produce a directory of links to Media Library items, starting from a given point. I have a document with a Media Picker on it to specify the starting point.

    I would like the XSLT to read down through the hierarchy and produce links to the next level down if it exists, or links to the documents (currently all PDFs).

    Any suggestions and / or pointers would be most welcome!

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 18, 2010 @ 15:11
    Dirk De Grave
    0

    Hi Gordon,

    Can use below for-each loop to get you started

    <xsl:for-each select="umbraco.library:GetMedia($startNode, true())/node">...</xsl:for-each>

    (Be aware though that it's querying the db quite heavily for each media item...)

    Also have a look at cultiv's media caching package (can be found in the project section)

    Cheers,

    /Dirk

  • dandrayne 1138 posts 2262 karma points
    May 18, 2010 @ 15:45
    dandrayne
    0

    See the attached xslt file for a big step in the right direction.  It could be cleaned up a lot and it currently relies on a media picker on the page to select a download folder, but it could be easily adapted to use macro parameters.

    Sorry I don't have much time to explain further!  These macros will create a nice list of documents with tags and file descriptions, filesize indicator and a an option to limit the list by tags.  It's probably a bit of overkill but a good start nonetheless

    Dan

    documentList.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"
    xmlns:tagsLib="urn:tagsLib"
    xmlns:pm="urn:pm"
    exclude-result-prefixes="pm msxml tagsLib 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:template match="/">


    <xsl:variable name="filter">
    <xsl:choose>
    <xsl:when test="string-length(umbraco.library:Request('tag')) &gt; 0">
    <xsl:value-of select="umbraco.library:Request('tag')"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="''"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <xsl:if test="string($currentPage/data [@alias='documentMediaFolder']) != ''">

    <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($currentPage/data [@alias='documentMediaFolder'], 'true')" />
    <!-- Is our folder a protected folder? If so, use Dirks PM tags -->


    <xsl:choose>
    <xsl:when test="string($mediaFolder/@nodeTypeAlias) = 'Protected Folder'">
    <!-- This is a protected folder -->
    <ul class="document_list">
    <xsl:choose>
    <xsl:when test="string($filter) = ''">
    <xsl:for-each select="$mediaFolder/node">
    <xsl:variable name="expiresOn">
    <xsl:choose>
    <xsl:when test="string(string(./data[@alias='docExpireDate'])) != ''">
    <xsl:value-of select="./data[@alias='docExpireDate']" />
    </xsl:when>
    <xsl:otherwise>
    <xsl:text>2999-11-10T00:00:00</xsl:text>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:if test="umbraco.library:DateGreaterThanToday($expiresOn) and pm:HasPermissionToDownload(current()/@id)">
    <li>
    <xsl:attribute name="class">
    <xsl:value-of select="current()/data [@alias='umbracoExtension']" />
    </xsl:attribute>
    <h3>
    <a target="_blank" href="{pm:GetDownloadLink(current()/@id)}">
    <xsl:value-of select="current()/@nodeName" /><xsl:text> </xsl:text>
    (<xsl:call-template name="FileSizeKb">
    <xsl:with-param name="fileSize" select="current()/data [@alias='umbracoBytes']" />
    </xsl:call-template>)
    </a>
    </h3>
    <xsl:if test="string(current()/data [@alias='fileDescription']) != ''">
    <p class="file_description"><xsl:value-of select="current()/data [@alias='fileDescription']" /></p>
    </xsl:if>
    <xsl:if test="string(current()/data [@alias='fileTags']) != ''">
    <xsl:variable name="tags" select="current()/data[@alias='fileTags']"/>
    <xsl:variable name="tagSplit" select="umbraco.library:Split($tags, ',')" />
    <p class="document_tags">
    <xsl:for-each select="$tagSplit/value">

    <xsl:variable name="tagSplitItem" select="." />
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)"/><xsl:text>?tag=</xsl:text><xsl:value-of select="$tagSplitItem" />
    </xsl:attribute>
    <xsl:value-of select="$tagSplitItem" />
    </a>
    <xsl:if test="position() != last()"><xsl:text> | </xsl:text></xsl:if>
    </xsl:for-each>
    </p>
    </xsl:if>
    </li>
    </xsl:if>
    </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
    <li class="currently_viewing">
    <xsl:text>You are currently viewing all items tagged with </xsl:text>
    <em>"<xsl:value-of select="$filter" />"</em>. <a href="{umbraco.library:NiceUrl($currentPage/@id)}">View all.</a>
    </li>
    <xsl:for-each select="$mediaFolder/node [contains(Exslt.ExsltStrings:lowercase(./data [@alias='fileTags']), Exslt.ExsltStrings:lowercase($filter))]">
    <li>
    <xsl:attribute name="class">
    <xsl:value-of select="current()/data [@alias='umbracoExtension']" />
    </xsl:attribute>

    <h3>
    <a target="_blank" href="{current()/data [@alias='umbracoFile']}">
    <xsl:value-of select="current()/@nodeName" /><xsl:text> </xsl:text>
    (<xsl:call-template name="FileSizeKb">
    <xsl:with-param name="fileSize" select="current()/data [@alias='umbracoBytes']" />
    </xsl:call-template>)
    </a>
    </h3>
    <xsl:if test="string(current()/data [@alias='fileDescription']) != ''">
    <p class="file_description"><xsl:value-of select="current()/data [@alias='fileDescription']" /></p>
    </xsl:if>
    <xsl:if test="string(current()/data [@alias='fileTags']) != ''">
    <xsl:variable name="tags" select="current()/data[@alias='fileTags']"/>
    <xsl:variable name="tagSplit" select="umbraco.library:Split($tags, ',')" />
    <p class="document_tags">
    <xsl:for-each select="$tagSplit/value">

    <xsl:variable name="tagSplitItem" select="." />
    <a>
    <xsl:if test="$tagSplitItem = $filter">
    <xsl:attribute name="class">
    <xsl:text>current</xsl:text>
    </xsl:attribute>
    </xsl:if>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)"/><xsl:text>?tag=</xsl:text><xsl:value-of select="$tagSplitItem" />
    </xsl:attribute>
    <xsl:value-of select="$tagSplitItem" />
    </a>
    <xsl:if test="position() != last()"><xsl:text> | </xsl:text></xsl:if>
    </xsl:for-each>
    </p>
    </xsl:if>
    </li>
    </xsl:for-each>
    </xsl:otherwise>
    </xsl:choose>
    </ul>

    </xsl:when>
    <xsl:otherwise>
    <!-- This is a normal folder -->
    <ul class="document_list">
    <xsl:choose>
    <xsl:when test="string($filter) = ''">
    <xsl:for-each select="$mediaFolder/node">
    <xsl:variable name="expiresOn">
    <xsl:choose>
    <xsl:when test="string(string(./data[@alias='docExpireDate'])) != ''">
    <xsl:value-of select="./data[@alias='docExpireDate']" />
    </xsl:when>
    <xsl:otherwise>
    <xsl:text>2999-11-10T00:00:00</xsl:text>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:if test="umbraco.library:DateGreaterThanToday($expiresOn)">
    <li>
    <xsl:attribute name="class">
    <xsl:value-of select="current()/data [@alias='umbracoExtension']" />
    </xsl:attribute>

    <h3>
    <a target="_blank" href="{current()/data [@alias='umbracoFile']}">
    <xsl:value-of select="current()/@nodeName" /><xsl:text> </xsl:text>
    (<xsl:call-template name="FileSizeKb">
    <xsl:with-param name="fileSize" select="current()/data [@alias='umbracoBytes']" />
    </xsl:call-template>)
    </a>
    </h3>
    <xsl:if test="string(current()/data [@alias='fileDescription']) != ''">
    <p class="file_description"><xsl:value-of select="current()/data [@alias='fileDescription']" /></p>
    </xsl:if>
    <xsl:if test="string(current()/data [@alias='fileTags']) != ''">
    <xsl:variable name="tags" select="current()/data[@alias='fileTags']"/>
    <xsl:variable name="tagSplit" select="umbraco.library:Split($tags, ',')" />
    <p class="document_tags">
    <xsl:for-each select="$tagSplit/value">

    <xsl:variable name="tagSplitItem" select="." />
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)"/><xsl:text>?tag=</xsl:text><xsl:value-of select="$tagSplitItem" />
    </xsl:attribute>
    <xsl:value-of select="$tagSplitItem" />
    </a>
    <xsl:if test="position() != last()"><xsl:text> | </xsl:text></xsl:if>
    </xsl:for-each>
    </p>
    </xsl:if>
    </li>
    </xsl:if>
    </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
    <li class="currently_viewing">
    <xsl:text>You are currently viewing all items tagged with </xsl:text>
    <em>"<xsl:value-of select="$filter" />"</em>. <a href="{umbraco.library:NiceUrl($currentPage/@id)}">View all.</a>
    </li>
    <xsl:for-each select="$mediaFolder/node [contains(Exslt.ExsltStrings:lowercase(./data [@alias='fileTags']), Exslt.ExsltStrings:lowercase($filter))]">
    <li>
    <xsl:attribute name="class">
    <xsl:value-of select="current()/data [@alias='umbracoExtension']" />
    </xsl:attribute>

    <h3>
    <a target="_blank" href="{current()/data [@alias='umbracoFile']}">
    <xsl:value-of select="current()/@nodeName" /><xsl:text> </xsl:text>
    (<xsl:call-template name="FileSizeKb">
    <xsl:with-param name="fileSize" select="current()/data [@alias='umbracoBytes']" />
    </xsl:call-template>)
    </a>
    </h3>
    <xsl:if test="string(current()/data [@alias='fileDescription']) != ''">
    <p class="file_description"><xsl:value-of select="current()/data [@alias='fileDescription']" /></p>
    </xsl:if>
    <xsl:if test="string(current()/data [@alias='fileTags']) != ''">
    <xsl:variable name="tags" select="current()/data[@alias='fileTags']"/>
    <xsl:variable name="tagSplit" select="umbraco.library:Split($tags, ',')" />
    <p class="document_tags">
    <xsl:for-each select="$tagSplit/value">

    <xsl:variable name="tagSplitItem" select="." />
    <a>
    <xsl:if test="$tagSplitItem = $filter">
    <xsl:attribute name="class">
    <xsl:text>current</xsl:text>
    </xsl:attribute>
    </xsl:if>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)"/><xsl:text>?tag=</xsl:text><xsl:value-of select="$tagSplitItem" />
    </xsl:attribute>
    <xsl:value-of select="$tagSplitItem" />
    </a>
    <xsl:if test="position() != last()"><xsl:text> | </xsl:text></xsl:if>
    </xsl:for-each>
    </p>
    </xsl:if>
    </li>
    </xsl:for-each>
    </xsl:otherwise>
    </xsl:choose>
    </ul>


    </xsl:otherwise>
    </xsl:choose>


    </xsl:if>


    </xsl:template>

    <xsl:template name="FileSizeKb">
    <xsl:param name="fileSize" />
    <xsl:if test="string-length($fileSize) &gt; 0">
    <xsl:if test="number($fileSize) &gt; 0">
    <xsl:choose>
    <xsl:when test="round($fileSize div 1024) &lt; 1"><xsl:value-of select="$fileSize" /> Bytes</xsl:when>
    <xsl:when test="round($fileSize div (1024 *1024)) &lt; 1"><xsl:value-of select="round($fileSize div 1024)" />kb</xsl:when>
    <xsl:otherwise><xsl:value-of select="round($fileSize div (1024 * 1024))" />Mb</xsl:otherwise>
    </xsl:choose>
    </xsl:if>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    documentListTags.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:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:ps="urn:percipientstudios-com:xslt"
    xmlns:gecko="urn:gecko-com:xslt"
    exclude-result-prefixes="msxml umbraco.library ps gecko">

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


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">


    <xsl:variable name="filter">
    <xsl:choose>
    <xsl:when test="string-length(umbraco.library:Request('tag')) &gt; 0">
    <xsl:value-of select="umbraco.library:Request('tag')"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:text>all</xsl:text>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <xsl:if test="string($currentPage/data [@alias='documentMediaFolder']) != ''">

    <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($currentPage/data [@alias='documentMediaFolder'], 'true')" />
    <xsl:variable name="mediaFolderTags">
    <xsl:for-each select="$mediaFolder/node [string(data[@alias='fileTags']) != '']"><xsl:value-of select="current()/data[@alias='fileTags']" />,</xsl:for-each>
    </xsl:variable>
    <xsl:variable name="resultSet" select="gecko:DistinctValues($mediaFolderTags)" />
    <h4 class="document_tags"><span>Filter by tags</span></h4>
    <ul class="all_tags">

    <li>
    <xsl:if test="$filter= 'all'">
    <xsl:attribute name="class">
    <xsl:text>current</xsl:text>
    </xsl:attribute>
    </xsl:if>
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)" />
    </xsl:attribute>
    All
    </a>
    </li>
    <xsl:for-each select="$resultSet//value">
    <xsl:sort select="@count" order="descending" />
    <xsl:sort select="text()" order="ascending" />
    <li>
    <xsl:if test="current()/text() = $filter">
    <xsl:attribute name="class">
    <xsl:text>current</xsl:text>
    </xsl:attribute>
    </xsl:if>
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)" />?tag=<xsl:value-of select="current()" />
    </xsl:attribute>
    <xsl:value-of select="current()" />
    <span>
    (<xsl:value-of select="current()/@count" />)
    </span>
    </a>
    </li>

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

    </xsl:if>



    </xsl:template>



    <msxml:script language="CSharp" implements-prefix="gecko">


    <msxml:assembly name="System.Web" />
    <msxml:using namespace="System.Web" />

    <![CDATA[


    public XmlDocument DistinctValues(String commaList) {

    string[] arr = commaList.Split(',');
    var distinctCount = new ArrayList(arr.Length);
    var distinctNames = new ArrayList(arr.Length);

    foreach(string str in arr)
    {
    if(string.IsNullOrEmpty(str))
    {
    continue;
    }
    var index = distinctNames.IndexOf(str);
    if (index >= 0)
    {
    var currentCount = (int)distinctCount[index];
    distinctCount[index] = currentCount + 1;
    }
    else
    {
    distinctNames.Add(str);
    distinctCount.Add(1);
    }
    }

    var outPut = "";
    for (var i = 0; i < distinctNames.Count; i++)
    {
    var name = distinctNames[i];
    var count = distinctCount[i];
    outPut += "<value count='" + count + "'>" + name + "</value>";
    }

    var xml = new XmlDocument();
    xml.LoadXml("<values>" + outPut + "</values>");
    return xml;
    }
    ]]>

    </msxml:script>

    </xsl:stylesheet>
  • dandrayne 1138 posts 2262 karma points
    May 18, 2010 @ 15:45
    dandrayne
    0

    Oops, should have added that it also uses Dirks protected media package, but this can be ripped out easily.

Please Sign in or register to post replies

Write your reply to:

Draft