I'm a bit of an XSLT whore, and even though I detest the
annoyingly verbose language - I have to admit parts of it are
starting to grow on me. Sort of like how the ugly girl in the
bar starts to get more attractive, the more alcohol you
consume. The same applies here, the more I use it parts of it
have started to become more attractive.
I think the only reason this is happening, is because over the
past 12 months we have built quite a few sites which have required
quite a lot of XSLT hacking coding. And
from doing the sites I have started to build up some good XSLT
tidbits which help with other sites, tidbits I have created myself
and whored from other peoples blogs or the forum. Because I
have these tidbits in various places, its better for me to collate
them here for future productivity. As I usually find myself
diving into an old site, thinking "Oooh I know I did something
like this for them, I'll grab the XSLT" which isn't the most
productive thing to do. [more]
So I am going to start listing useful, often used XSLT snippets
on this page so I can quickly reference back to them - And as I
mention above, I by no means AT ALL take any kudos for the code as
I most probably got it from the forum or a random blog post.
If I can remember where I stole got the code from I
will of course credit the site.
Recursive XSLT Lookup
This I got from the old forum
<xsl:value-of select="$currentPage/ancestor-or-self::node [string(data[@alias='YOURPROPERTYNAME'])!=''] [position()=1] /data[@alias='YOURPROPERTYNAME']"/>
Loop Through Ultimate Picker Results
This I got from the awesome Lee Keheller
<xsl:variable name="preNodes">
<xsl:variable name="relatedContent" select="$currentPage/data[@alias='PROPNAME']" />
<xsl:variable name="nodeIds" select="umbraco.library:Split($relatedContent, ',')" />
<xsl:for-each select="$nodeIds/value">
<xsl:copy-of select="umbraco.library:GetXmlNodeById(.)"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="nodes" select="msxml:node-set($preNodes)/node" />
<xsl:if test="count($nodes) > 0">
<ul>
<xsl:for-each select="$nodes">
<li><a href="{umbraco.library:NiceUrl(@id)}">{@nodeName}</a></li>
</xsl:for-each>
</ul>
</xsl:if>
Display Image Using ImageGen & Media Picker
<xsl:if test="string(data [@alias = 'MEDIAPICKERNAME']) != ''">
<img src="/umbraco/imagegen.ashx?image={umbraco.library:GetMedia(number(data [@alias = 'MEDIAPICKERNAME']), 'false')/data [@alias = 'umbracoFile']}&width=250" alt="{@nodeName}" />
</xsl:if>
List All Tags On The Current Node
<xsl:for-each select="tagsLib:getTagsFromNode($currentPage/@id)/tags/tag">
<xsl:value-of select="."/><xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
Get All Tags By Group Name
This is from Blog4Umbraco
<ul>
<xsl:for-each select="tagsLib:getAllTagsInGroup('TAGGROUPNAME')/tags/tag">
<li>
<a href="{umbraco.library:NiceUrl(@id)}?filterby={current()}"><xsl:value-of select="current()"/></a> (<xsl:value-of select="@nodesTagged"/>)
</li>
</xsl:for-each>
</ul>
List Sub, Sub Nodes
<ul>
<xsl:for-each select="$currentPage/node">
<li>
<a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
<xsl:if test="count(./node) > 0">
<ul>
<xsl:for-each select="./node">
<li>
<a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</xsl:for-each>
</ul>