Skip to content

Instantly share code, notes, and snippets.

@greystate
Created May 20, 2011 21:11
Show Gist options
  • Select an option

  • Save greystate/983809 to your computer and use it in GitHub Desktop.

Select an option

Save greystate/983809 to your computer and use it in GitHub Desktop.
Refactoring of http://snipt.org/xKr (included below)
<?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"
xmlns:make="urn:schemas-microsoft-com:xslt"
xmlns:math="urn:Exslt.ExsltMath"
exclude-result-prefixes="umb math make"
>
<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]" />
<xsl:variable name="totalTagSizes" select="6" />
<xsl:variable name="searchTags" select="$siteRoot/publicSearch/publicSearchTags/publicSearchTag" />
<xsl:template match="/">
<xsl:variable name="xml">
<tags>
<xsl:for-each select="$searchTags">
<xsl:variable name="relations" select="umb:GetRelatedNodesAsXml(@id)/relations" />
<tag name="{@nodeName}" id="{@id}" count="{count($relations/relation)}" />
</xsl:for-each>
</tags>
</xsl:variable>
<xsl:variable name="xmlNodeSet" select="make:node-set($xml)" />
<xsl:variable name="totalRelatedNodes" select="sum($xmlNodeSet/tags/tag/@count)" />
<xsl:variable name="tagFactor" select="$totalTagSizes div math:max($xmlNodeSet/tags/tag/@count)" />
<xsl:apply-templates select="$xmlNodeSet/tags/tag[@count &gt; 0]">
<xsl:with-param name="factor" select="$tagFactor" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="tag">
<xsl:param name="factor" select="1" />
<li>
<span>
<xsl:value-of select="@count" />
<xsl:text> blogs are tagged with </xsl:text>
</span>
<a href="{umb:NiceUrl(@id)}" class="{concat('tag', round($factor * @count), 'x')}">
<xsl:value-of select="@name" />
</a>
</li>
</xsl:template>
</xsl:stylesheet>
<?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:msxsl="urn:schemas-microsoft-com:xslt"
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.dates="urn:ucomponents.dates"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ucomponents.dates ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<!-- Generate Tag Cloud, Laurie 2011 -->
<!-- User Varibles -->
<!-- Total Tag Sizes -->
<!-- This is where you set how many classes for different tag sizes you want -->
<xsl:variable name="totalTagSizes" select="'6'" />
<xsl:template match="/">
<!-- Create the XML required -->
<xsl:variable name="xml">
<!-- Loop Over Tags -->
<tags>
<xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc and @level= 1]/publicSearch/publicSearchTags/publicSearchTag">
<xsl:variable name="count">
<!-- How many times has the tag been used? -->
<xsl:for-each select="umbraco.library:GetRelatedNodesAsXml(@id)/relations/relation">
<xsl:if test="position() = last()">
<xsl:value-of select="position()" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- Process each tag and create XML -->
<tag>
<xsl:attribute name="name">
<xsl:value-of select="@nodeName"/>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="count">
<xsl:choose>
<xsl:when test="$count !=''">
<xsl:value-of select="$count"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</tag>
</xsl:for-each>
</tags>
</xsl:variable>
<!-- Convert XML into Nodeset -->
<xsl:variable name="xmlNodeSet" select="msxsl:node-set($xml)" />
<!-- Total Related Nodes - Not Used - Could be useful -->
<xsl:variable name="totalRelatedNodes" select="sum($xmlNodeSet/tags/tag/@count)" />
<!-- Tag Factor -->
<xsl:variable name="tagFactor" select="$totalTagSizes div Exslt.ExsltMath:max($xmlNodeSet/tags/tag/@count)" />
<xsl:call-template name="displayTags">
<xsl:with-param name="xmlNodeSet" select="$xmlNodeSet" />
<xsl:with-param name="tagFactor" select="$tagFactor" />
</xsl:call-template>
</xsl:template>
<xsl:template name="displayTags">
<xsl:param name="xmlNodeSet" />
<xsl:param name="tagFactor" />
<!-- Now we're cooking with gas! -->
<xsl:for-each select="$xmlNodeSet/tags/tag [@count &gt; 0]">
<li>
<span>
<xsl:value-of select="@count" />
<xsl:text> blogs are tagged with </xsl:text>
</span>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:attribute name="class">
<xsl:call-template name="getTagClass">
<xsl:with-param name="tagSize" select="round($tagFactor * @count)" />
</xsl:call-template>
</xsl:attribute>
<xsl:value-of select="@name" />
</a>
</li>
</xsl:for-each>
</xsl:template>
<!-- Work out the Correct Font Size -->
<xsl:template name="getTagClass">
<xsl:param name="tagSize" />
<xsl:text>tag</xsl:text>
<xsl:value-of select="$tagSize" />
<xsl:text>x</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment