usingc#codeinxsltransformations

Using C# code in XSL Transformations

Introduction

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.

Example of using C# code in XSL Transformations

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.

if
(!string.IsNullOrEmpty(aDateTime)){// Try to convert it to
DateTime and format it with format specifierDateTimeFormatedString =
DateTime.Parse(aDateTime).ToString(DateTimeFormat);}return DateTimeFormatedString;}catch{

The aspx page task is only
to load the Contacts.xml and Contacts.xsl files and transform the
XML using XslCompiledTransformclass. 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 inscript 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:scriptlanguage="C#"implements-prefix="my-scripts">

<![CDATA[

]]></msxsl:script>

According to the MSDN
library,thelanguage attribute is not mandatory,
but if specified, it must have
one of the followingvalues: C#, VB, JScript,
JavaScript, VisualBasic, or CSharp. If not specified, the language defaults to
JScript.

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 urn:schemas-microsoft-com:xslt.Because the msxsl:script element belongs to the
namespace urn:schemas-microsoft-com:xslt,
the style sheet must include the namespace declaration xmlns:msxsl=urn:schemas-microsoft-com:xslt.

if
(!string.IsNullOrEmpty(aDateTime)){// Try to convert it to DateTime and format it with format specifierDateTimeFormatedString =
DateTime.Parse(aDateTime).ToString(DateTimeFormat);}return DateTimeFormatedString;}catch{// On error returns the original stringreturn aDateTime;}}

In the XSLT
file, I use FormatDate method by just calling the it and passing the
required parameters

<xsl:value-ofselect="my-scripts:FormatDate(certExpDate,'F')"/>

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