Umbraco/Samples and Articles/XSLT/Extend Xsl

< Umbraco‎ | Samples and Articles‎ | XSLT

Extend XSL using Javascript, VB or C#Edit

Originally from

When developing using xsl, the language sometimes falls short or its difficult and time consuming to accomplish what is easily done in other programming languages.

Answering a question in the umbraco mailing list, revealed that it wasn't common knowledge that you could extend xsl with inline code of your favorite programming language, but it is in fact very easy to do so.

An example using inline server interpreted Javascript.

<?xml version="1.0" encoding="UTF-8"?>
                exclude-result-prefixes="msxml math umbraco.library"> 
                <xsl:output method="html"/> 
                                                function random(){ 
                                                        return Math.random();
                                <xsl:template match="/">
                                                        select="$nodeset[floor(math:random() * (last() -1)) + 1]"/> 

This above example shows how to get a random node from a nodeset, using a wrapper to the "Math.random()" function from Javascript, in order to generate a random seed.

The javascript is executed serverside - in fact any defined function is compiled to MSIL (wrapped in a class definition), and thus should have little effect on the general performance - other scenarios could result in performance overheads, but - in the soon to be released Umbraco v. 2.1 - it possible to cache the output of your macroes and therefore it should be possible to maintain high performance and at the same time flexibility in the development.

You can use different programming languages, a complete list and further information can be found at

Author: Kasper Bumbech Andersen