Create XsltExtension and using in Umbraco 4.5

This post will explain how to create and use Xslt Extension in Umbraco 4.5.We will add few basic function we always needs in everyday coding.
First of all, we need to create a class library project (you can add this project to your solution or create a new solution). All we need is to add references to umbraco dlls. Generally umbraco, cms, interfaces and businesslogic will cover all our needs.

Now we will add a new class (for example, Library in Library.cs) and add two  methods: QueryString and ToUpper:

public class Library
{
 public String QueryString(String key, String defaultValue)
 {
   return HttpContext.Current.Request.QueryString[key] ?? defaultValue;
 }

 public String ToUpper(String inputString)
 {
   return inputString.ToUpper();
 }
}

Next, we need to put the output assembly in the Umbraco bin folder, then open the file /config/xsltExtension.config and add this line before the </XsltExtensions> closing tag:

<ext
  assembly="My.Umbraco.XsltExtensions"
  type="My.Umbraco.XsltExtensions.Library"
  alias="my" />

Finally, in our xslt files, we have to modify the xsl:stylesheet opening tag:

<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:my="urn:my"
  exclude-result-prefixes="msxml umbraco.library my">

and use our methods in this way:


where exampleText is a dataType “textstring” of the current page document type.


Leave a Reply

*