How To Do Simple XSLT Transforms With Cocoon

Arguably the simplest Cocoon application is one that simple serves files that are dynamically generated using XSLT.

This HOW-TO assumes that

The only additional information you need is how to configure the Sitemap to do a transformation, this is described in the following sections.

The XSLT Transformer

Cocoon provides an XSLT transformer component which is already configured in the example Sitemap. If you're starting from a MinimalSitemapConfiguration then here's the declaration that you need:

<map:transformers default="xslt">
   <map:transformer name="xslt" src="org.apache.cocoon.transformation.TraxTransformer"/>
</map:transformers>

Note: if you're using a sub-sitemap, then you don't need to configure the transformer component again if it's already defined in the root or parent sitemap. For more information see (MinimalSitemapConfiguration and UnderstandingCocoonMounts)

The Basic Pipeline

Here's a basic pipeline that responds to requests for http://your.server.com/cocoon/my.html
by parsing a file called my.xml, transforms it with a stylesheet my.xsl, and serializes the results as HTML:

<map:pipeline>
  <map:match pattern="my.html">
    <map:generate src="my.xml"/>
    <map:transform src="stylesheets/my.xsl"/>
    <map:serialize/>
  </map:match>
</map:pipeline>

Things to note here:

If you want to serialize the results as XML, then use the XML Serializer:

<map:serialize type="xml"/>

If you want to serialize the results as XHTML (the XHTML Doctype will be automatically added), then use the XHTML Serializer:

<map:serialize type="xhtml"/>

Passing Parameters

It's possible to have Cocoon pass parameters to the XSLT processor that can be accessed from your stylesheet.

For this to work correctly, you must have first declared the parameter in the XSLT stylesheet using the xsl:param element.

Fixed Parameters

The <map:parameter/> element can be used to pass a fixed value to a stylesheet, as follows:

<map:transform src="stylesheets/my.xsl">
  <map:parameter name="your-parameter-name" value="your-parameter-value"/>
</map:transform>

Sitemap Parameters

You can also use the <map:parameter/> element to pass dynamic Sitemap parameters to the XSLT processor using the curly brace syntax.

<map:transform src="stylesheets/my.xsl">
  <map:parameter name="your-parameter-name" value="{1}"/>
</map:transform>

These parameters are set by Matchers and/or Actions.

Request Parameters

You can pass request parameters to the XSL processor, as follows:

<map:transform src="stylesheets/my.xsl">
  <map:parameter name="use-request-parameters" value="true"/>
</map:transform>

When declaring the Components (see XSLT Transformer, above) you can tell it to automatically use request parameters for all transformations like this:

<map:transformer name="xslt" src="org.apache.cocoon.transformation.TraxTransformer">
   <use-request-parameters>true</use-request-parameters>
</map:transformer>

Note that passing request parameters in this way can affect how Cocoon Caching the results of your transformation.

Further Reading