XSLT Mediator

The XSLT Mediator 

With this eXtension we want to extend the modular templating framework with a XSLT mediator enabling the use of XSLT templates in the modular templating framework, as a number of people liked it being available in the old-school template model as well.

Using XSLT for your templates has pros and cons, but given sufficient knowledge and not too-often changing designs the power of XSLT has for sure benefits in areas like string and format manipulation, linked component handling. performance and Webforms.

Enter the XSLT Mediator:

The modular templating framework can handle templates in any technology by the use of mediators, that load, control and execute both the template and the underlying techonology, and give it access to the underlying content package.

The XSLT Mediator loads the XSLT TBB into an XslCompiledTransform object and use the Component, Page or a specific package xml item as the input for the transformation.

Additionally, it will load a helper object into the transformation of which methods can be called during the transformation for example to retrieve a list of Tridion items or to format a date string.

To be able to refer to Tridion items as xml documents an implementation of the XmlUrlResolver is used, this allows the Mediator to return the XML of an item based on its TCM URI in a similar way to the Protocol Handler.

Another great benefit of using the Mediator is the ability to create reusable XSLT templates in TBBs which can be used by other templates. This is a standard XSLT approach and is available by using the <xsl:include> instruction. An important feature is that Tridion will manage these includes in the same manner it does for all includes meaning the Where Used functionality and deletion prevention, etc. 


Configuration

To enable the Mediator the Tridion CM configuration requires a small modification:

The file in the folder [InstallationFolder]\Config is called Tridion.ContentManager.config

Preferably create a backup of this file and then open it in your preferred text editor and navigate to the <tridion.templating> element, then the <mediators> elment and add the following line:

<mediator matchMIMEType="text/xml" type="Tridion.Extensions.Mediators.XsltMediator" assemblyPath="D:\Tridion\bin\Tridion.Extensions.Mediators.dll" />

If you changed the namespace or assembly name or path make sure that change is reflected in this configuration statement. 


To start using the registered Mediator restart the Tridion Content Manager COM+ application, IIS and the Publisher service. 


Usage 

To use the Mediator you should start by writing your XSLT template(s), this is done in same way it has always been done using an XSLT TBB:

Make sure to associate your templates with the XSLT Transformation Parameter Schema (discussed below).

Save and close the template.

In the Template Builder (if already configured the XSLT Mediator) you will find your XSLT Template in the Building Blocks list, simply drag your template to the main work area of the Template builder and essentialy you're done. You can now test your compound template and see whether the desired results are produced.

In the image above you can see the desired result is a html fragment which can be included on the page.

Setting the Parameters

The XSLT Transformation Parameter Schema includes a few useful fields which can be used for the transformation.


Input Item Name: The name of the item in the package to use as the input for the transformation. If left empty the Mediator will use the Component
item if used as part of a compound Component Template or the Page if used as part of a compound Page Template

Output Item Name
:The name of the item to modify or create in the package. If left empty the Mediator will use the "Output" item name.

Get Parameters from Package
: If set to True the Mediator will use all package items as transformation parameters (passed to the transformer object using an XsltArgumentList object). Default is False.

Package Items Name Prefix
:If previous parameter is set to true, this field supplies the Mediator with a name prefix for the package items to use as parameters. For example if the prefix is "_par". Items in the package named "_parDate" or "_parLink" will be used while the rest of the package items will not. The Mediator will remove the prefix from the name for the parameters it passes to the Trasnformer.

Output Format Type
: The Content Type the Mediator will use for the Output item. Default is XHTML. The last option "Get From Template" will attempt to   
retrieve the type based on the output method defined in the xslt itself.

Xslt Template Helper

The XSLT Template Helper is a class which is used to provider custom functionality to the Transformation process. 

The Mediator makes sure that an instance of the helper class is passed to the transformer. 

From the xslt code it is then possible to call the Helper's public methods. 


Currently these methods are: 

XmlDocument getListItems(String uriOrgItem)
Generic XSLT helper function to retrieve items from within an OrganizationalItem List returned as an XmlDocument

XmlDocument GetMultimediaInfo(string uri) 
Returns xml with information about the MM component such as the mime type or extension

String getFormatedDate(String inputDate, String dateFormat) 
Generic XSLT helper function to format date strings using standard format strings
Date returned as a string 


The helper can be extended with more methods or the Mediator can be extended to use a more pluggable approach to the helper class or classes.

The Mediator registers the helper with a specific namespace which is required to be stated in the xslt in order to be able to use it. The namespace is: "http://www.sdltridion.com/ps/XSLTHelper". In the XSLT it should then be registered like so: xmlns:TridionHelper="http://www.sdltridion.com/ps/XSLTHelper" where TridionHelper is a prefix which can be interchanged with any other unique name. 

Where Used and Uses

As stated before, using XSLT Includes is excellent for modularity, reuse and managing your XSLT code and the good news are that this is supported by Tridion Template Handler so the includes become managed links protecting the included templates from being deleted while being used and showing in the Where Used and Uses dialogs. For example: 

Conclusion 

The XSLT Mediator gives an excellent alternative to Dreamweaver templates but not necessarily replaces them altogether, its perfectly fine to use the two in conjunction with one another. 

The out of the box DW mediator has some functionality that the XSLT mediator doesn't such as pushing images into the package automatically. 

The XSLT transformation is also well performing, executing much faster than the DW one. Leaving the heavy lifting to the XSLT templates is a good choice, especially when many linked, embedded or multivalued fields are in use. 


A word of caution: The Mediator has been tested quite thoroughly but there is no guarantee it will perform similarly under all circumstances and on all environments.
It is strongly recommended that the Mediator should be tested extensively before applied on a Production environment.

Version 2011 (1.3)

  • Added the positioning variables (by Chris Summers)

  • Added check against duplicate items in the argument list passed to the transformation to avoid exceptions

  • Added extension object initialization for helpers that use constructors accepting Engine and Package instances

  • Minor overall code improvement and enhanced efficiency

To learn more about using extension objects in XSLT and .NET see: Extending XSLT using C# or How To from Microsoft. Which allow a way to extend the XSLT code with logic which is very difficult or impossible to implement purely in XSLT.


Developer
:    Yoav Niran
Company
License agreement
Download package
Tags
:    Templating,  2011,  XSLT,  .NET,  Modular Templates
Version
:   1.3
Source
:   Yes

About the Author
Yoav Niran
Senior Technical Consultant

Yoav joined Tridion in 2006 and is working in the Professional Services Maidenhead office as a senior technical consultant. 

He has been involved in numerous implementations, integrations and development of packaged services.

Yoav has much experience in .NET technologies and ASP.NET web development in particular.

He has also been the technical lead on the reconstruction of the SDL Tridion World website.

For more technical articles from Yoav go to: http://yoavniran.wordpress.com

SDL CMT division

Related building blocks

See these eXtensions related to the XSLT Mediator