XSLT (Extensible StyleSheet Language Transformations) is used to transform XML documents into documents with different structure and format. XSLT includes over 100 built-in functions. But sometimes you may need an additional functionality that is not supported by XSLT. In these situations, XSLT scripting allows you to include embedded scripting using a script element. This article will present an example of using a custom C# script code in XSLT.
The XSL transformation will be performed using the XslCompiledTransform class. The XslCompiledTransform class is an XSLT processor that supports the XSLT 1.0 syntax. It is a new implementation and includes performance gains when compared to the obsolete XslTransform class.
It will be transformed the following xml:
<?xml version="1.0" encoding="UTF-8"?>
The xml will be transformed in simple Html using the following XSLT:
<?xml version="1.0" encoding="utf-8"?>
public static string FormatDate(string
aDateTime, string DateTimeFormat)
// On error returns the original
Finally, for the transformation and rendering the generated Html I use a simple aspx page:
<%@ Page Language="C#"
string XsltFile = Request.PhysicalApplicationPath + @"App_Data\Contacts.xsl";
the XSL file
The aspx page task is only to load the Contacts.xml and Contacts.xsl files and transform the XML using XslCompiledTransform class. Therefore, I will not concentrate on this part.
The interesting part is the XSL file Contacts.xsl. This is where it is included the C# custom code.
The custom methods are defined in script element. When the style sheet is loaded, any defined functions are compiled to Microsoft intermediate language (MSIL) by being wrapped in a class definition.
<msxsl:script language="C#" implements-prefix="my-scripts">
The implements-prefix attribute is mandatory. This attribute is used to declare a namespace and associate it with the script block. The value of this attribute is the prefix that represents the namespace.
msxsl is a prefix bound to the namespace
Also, this is the place to include the namespace associated with the script block. In the example it is my-scripts.
Now it’s time for the custom code in the script block. In the example I define the method FormatDate and use it for converting and formatting the XML date string:
In the XSLT file, I use FormatDate method by just calling the it and passing the required parameters
The “certExpDate” value is retrieved from the XML “certExpDate” element.
You can also include custom code from different assembly.
By default only the following namespaces are supported: System, System.Collection, System.Text, System.Text.RegularExpressions, System.Xml, System.Xml.Xsl, System.Xml.XPath, and Microsoft.VisualBasic
In the example, I include the System.Web assembly and namespace.
In the example I define the method UrlEncode that use the class HttpUtility from the System.Web assembly.
NOTE: If you use msxsl:assembly with the obsolete class XslTransform you may receive the following error:“The 'msxslcript' element cannot be empty.”