User Defined Functions in an XSL Style Sheet




XSLT is a powerful language that can be used to transform XML into other formats such as HTML, flat-
file, and even other forms of XML; however, XSLT does not contain all of the power found in languages
such as C# or VB.NET. In situations where you need to perform functions that XSLT does not provide
out of the box, you can resort to creating user-defined functions.

Some of the situations where you might need to resort to user-defined functions are:

  Call custom business logic

  Perform different actions depending on permissions

  Perform complex formatting for dates, strings, etc.

  Call a Web service

  Call methods of the classes in the .NET Framework class library

The XslCompiledTransform class provides two primary mechanisms for creating user defined functions.
XSL style sheets can embed script functions written in C#, Visual Basic .NET or JScript.NET within msxsl:
script elements, which can then be invoked from within the style sheet just as if they were regular XSLT
functions. You see an example of this approach in the “Embedding Scripts Inside the XSL Style Sheet”
section later in this chapter. Another approach is to use XSLT extension objects. Extension objects are
regular objects whose public methods are accessible from a style sheet once the objects are added to the

XslCompiledTransform class through the AddExtensionObject() method.

An important point of XSLT is that transformation should never provide side-
effects. When loading an XSLT extension object, there are times where there might
be a problem due to unloading of appdomains for assemblies used in extension
objects. Because of this, it is recommended that you perform pre-processing instead
of relying on a post-processing approach using extension objects.


learn guitarphysics learnteliphonyxmlphysicsenjoylife