Calling a .NET C# class from XSLT

If you've ever worked with XSLT, you'd know that it's pretty limited when it comes to its programming capabilities. Try writing a for loop in XSLT and you'd know what I mean. XSLT is not designed to be a programming language so you should never put too much programming logic in your XSLT. That code can be a pain to write and maintain and so it should be avoided at all costs. Keep your xslt simple and put any complex logic that your xslt transformation requires in a class.

Here is how you can create a helper class and call that from your xslt. For example, this is my helper class:

 public class XsltHelper

    {

        public string GetStringHash(string originalString)

        {

            return originalString.GetHashCode().ToString();

        }

    }

And this is my xslt file(notice the namespace declaration that references the helper class):

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ext="http://MyNamespace"\>

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

    <xsl:template  match="/">The hash code of "<xsl:value-of select="stringList/string1" />" is "<xsl:value-of select="ext:GetStringHash(stringList/string1)" />".

    </xsl:template>

</xsl:stylesheet>

Here is how you can include the helper class as part of the transformation:

string xml = "test";

            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(xml);

            XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();

            xslCompiledTransform.Load("XSLTFile1.xslt");

            XsltArgumentList xsltArgs = new XsltArgumentList();           

            xsltArgs.AddExtensionObject("http://MyNamespace", Activator.CreateInstance(typeof(XsltHelper)));

            using (FileStream fileStream = new FileStream("TransformResults.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))

            {

                // transform the xml and output to the output file ...

                xslCompiledTransform.Transform(xmlDocument, xsltArgs, fileStream);               

            }

This article is part of the GWB Archives. Original Author: HanSolo

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.