Home > Programming, Tridion > Implementing the XSLT Mediator – Part 1

Implementing the XSLT Mediator – Part 1


Contents

Introduction

In the next series of articles I plan on showing how to use the XSLT Mediator which Im currently maintaining as an eXtension available on SDLTridion World.

The Mediator can be downloaded from the XSLT Mediator Page

An explanation why should you use the XSLT Mediator can be found at the aforementioned download page, so I won’t go into that, ill simply show how it can be used.

This is the first part in a series of at least 3 articles; in following articles I will mainly be discussing how to extend the XSLT transformation with .NET code.

This article assumes basic knowledge of XSLT and basic Tridion experience in templating.

Setting up the Mediator

In the Mediator zip file you will find the assembly DLL which can be used as is as well as the source code of the mediator project.

You should be able to simply drop the DLL somewhere on the Content Manager Server to begin using it but you may choose to first use the source code either to build it against a different version of Tridion (currently that’s 5.3GA) or if you wish to add extension objects to the assembly (will be discussed on the next article in this series).

The simple setup requires an edit to the Tridion configuration file, which is located in the Config folder under the Tridion installation folder (i.e.: C:\Program Files\Tridion\Config) and the file name is Tridion.ContentManager.config.

Open the file for editing and insert the following XML fragment in the <mediators> element:

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

If you’re using the binary as it is supplied then there is no need to change the ‘type’ attribute but make sure the ‘assemblyPath’ attribute is pointing to the right location of where you placed the DLL file.

To begin using the Mediator you should restart the following: Tridion COM+ package (Component Services), IIS, Tridion Publisher service and any open Template Builder instance.

Next step is to create the parameters schema which will be used with the XSLT TBBs we will be creating.

In the zip file you will find XSLT_Transformation_Parameters.xsd, In Tridion create a new parameters schema and use the upload button in the source tab to load the contents of the included XSD file:

XSLT transformation parameters schema

XSLT transformation parameters schema

That’s it. This is all the setting-up needed for now.

Creating an XSLT Component Template

Now you’re ready to create your first XSLT TBB which can be used in a compound template.

Note: For a long time it was possible to create XSLT component templates in Tridion, this has not changed. XSLT page templates however are not supported so in order to create these, the Mediator is required. The Mediator has another advantage of being able to incorporate managed (.NET) code into XSLT templates (will be discussed in a later article).

Lets create a simple schema which you will be able to use to test your new XSLT template with.
(The schema is included in the downloadable for this article)

Simple Schema

Simple Schema

The schema contains a title text field, a body rich text field, a date field and a multimedia link for an image.

Create a new component based on this schema and populate its fields with some values.
Based on this schema we will start with a simple XSLT TBB which we can extend later on:


<?xml version="1.0" encoding="utf-8"?>


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:simple="uuid:3CC625ED-34E6-4B51-BDD8-72BBD0D71469" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
exclude-result-prefixes="msxsl simple">

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

<xsl:template match="/">
<xsl:element name="p">

<xsl:apply-templates select="tcm:Component/tcm:Data/tcm:Content/simple:Content"/>
</xsl:element>
</xsl:template>

<xsl:template match="simple:Content">

<xsl:element name="h3">

<xsl:value-of select="simple:title"/>

</xsl:element>

<xsl:element name="div">

<xsl:copy-of select="simple:body"/>

</xsl:element>

<xsl:element name="div">

Publish Date: <xsl:value-of select="simple:pubDate" />

</xsl:element>

<xsl:element name="img">

<xsl:attribute name="src">

<xsl:value-of select="simple:image/@xlink:href"/>

</xsl:attribute>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

If you’re familiar with doing normal XSLT component templates you will notice the declaration of the “simple” namespace prefix at the top, this is necessary to be able to output the component’s content. The uuid:3CC625ED-34E6-4B51-BDD8-72BBD0D71469 namespace is copied from the schema we’ve just created (notice that each new schema receives a new target namespace by default which is in this long string format).

The rest of the template is very straight forward and just outputs the field values from the component.

In Tridion create a new TBB and copy the above code into the source tab. In the general tab make sure to add the parameters schema created before then save & close the template:

XSLT TBB with parameter schema

XSLT TBB with parameter schema

To use this new template open the Template Builder and create a new compound component template:

Template Builder Compound CT with XSLT TBB

Template Builder Compound CT with XSLT TBB

You can now run the template with the component you created earlier, the result should be something like this:

Run Compound CT in Template Builder

Run Compound CT in Template Builder

Note: Above I mentioned that I wont go into the merits of using the XSLT Mediator but one very important thing to notice is how quick it is in comparison to the Dreamweaver Mediator, obviously this example cannot be taken into account but even in real life templates, XSLT ones out run their DW counterparts by hundreds or percent.

There is no need to fill in any of the parameters attached to the XSLT Template but lets take a moment to review these:

Input Item Name – This is the item in the package to use as the input xml for the transformation.The default item which will be used for a component template is the item with name ‘Component’, for a page template it will be the ‘Page’ item.

Output Item Name – This is the name of the item the Mediator will create in the package with the result of the transformation; the default is ‘Output’.

Get Parameters from Package – A true/false value determining whether the Mediator should include items in the package as parameters for the transformation. Default is ‘true’.

For performance purposes it is recommended to change this to ‘false’ unless you require all of the items to act as parameters or plan on using specific items by specifying a prefix.

Package Items Name Prefix – The value of this parameter tells the mediator to only use items in the package which have the specified prefix. Presuming, the value of the previous parameter is ‘true’.

For example all items in the package with a prefix of ‘_par’ will be used if this value is specified.
The mediator will strip the prefix from the actual parameter name when its added to the transformation.

Output Format Type – Which content type to use for the result item created by the Mediator Default is ‘XHTML’.


Getting back to our new template:

Notice the broken image in the last  screen shot, by using some of the default TBBs provided by Tridion we can easily sort this out:

Compound Template with Image

Compound Template with Image

The order in which to place these TBBs (Extract, Publish, Resolve) is critical for the image to be published and displayed correctly.

Component Linking

The XSLT Mediator supports “linking” natively in a very similar way to how it was accomplished in XSLT CTs – by use of the document() function.

To demonstrate this ability I will use a metadata field set on the multimedia component to set the alt attribute of the image element in the resulting html. First the modified XSLT TBB:


<?xml version="1.0" encoding="utf-8"?>


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:simple="uuid:3CC625ED-34E6-4B51-BDD8-72BBD0D71469" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
xmlns:image="http://www.tridion.com/ContentManager/5.0/DefaultMultimediaSchema"
exclude-result-prefixes="msxsl simple">

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

<xsl:template match="/">
<xsl:element name="p">

<xsl:apply-templates select="tcm:Component/tcm:Data/tcm:Content/simple:Content"/>
</xsl:element>
</xsl:template>

<xsl:template match="simple:Content">

<xsl:element name="h3">

<xsl:value-of select="simple:title"/>

</xsl:element>

<xsl:element name="div">

<xsl:copy-of select="simple:body"/>

</xsl:element>

<xsl:element name="div">

Publish Date: <xsl:value-of select="simple:pubDate" />

</xsl:element>

<xsl:element name="img">

<xsl:attribute name="alt">
<xsl:value-of select="document(simple:image/@xlink:href)/tcm:Component/tcm:Data/tcm:Metadata/image:Metadata/image:altText"/>
</xsl:attribute>


<xsl:attribute name="src">
<xsl:value-of select="simple:image/@xlink:href"/>

</xsl:attribute>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

The interesting additions are in red, first add the namespace of the multimedia schema, in this case this is the default MM schema, We use  ‘image’ as the prefix.

Then add a definition for a second attribute to the ‘img’ element, in the example we use the document function to load the entire multimedia component’s XML by passing the xlink:href attribute of the link field, this attribute’s value is the TCM URI of the image  component. (In the content component the link is stored like this: <image xlink:type=”simple” xmlns:xlink=”http://www.w3.org/1999/xlink&#8221; xlink:title=”chrome” xlink:href=”tcm:42-15992″ />)

We then use an inline XPATH statement to get to the altText metadata field.

The result of this can be seen here:

XSLT CT - component links

XSLT CT - component links

With the document function we can successfully retrieve information from any other Tridion item as long as we know its ID (TCM URI).

Conclusion

It is very easy to set up and start using the XSLT Mediator, it provides both flexibility and speed which are not catered so well by the Dreamweaver mediator and for customers wishing to make use of an XSLT based solution it gives the possibility of using XSLT templates for components and pages alike.

However this article does not show some of these advantages, In the second part of this series I will look into:

  • Extending the XSLT transformation with extension objects
  • Using Extension objects to publish binaries, render component presentations, etc.
  • Creating an XSLT Page Template

Source Code

You can download the example XSLT and Schema here.

  1. September 2, 2011 at 10:16 | #1

    Hi Yoav,

    Great to see more Tridion knowledge out here ‘the wild’ instead of just the tridion forum.

    I’ve created my own custom XSLT helper class from the IXSLTHelper interface. The helper is loaded from the .NET assembly stored in Tridion. This works great in Template Builder and when publishing items. However, when I do a preview in the GUI I get the following error:

    “Cannot find the script or external object that implements prefix ‘http://www.tridion.com/xslthelper’.”

    The namespace here is the one I used for the ‘TridionXSLTHelperNS’ attribute.
    It appears that the “GetHelpersFromTridion” method is not getting called (or fails) when using preview mode. Have you seen this error before? Any ideas on how to fix this?

  1. July 17, 2009 at 13:23 | #1
  2. July 30, 2009 at 05:06 | #2
  3. October 28, 2010 at 14:01 | #3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 315 other followers

%d bloggers like this: