Making Parameters Available in a Transformer

by Andreas Hartmann

Setting up the Transformer to use Parameters

If you want to access request parameters in a XSL stylesheet, you can enable the use-request-parameters value of the TraxTransformer. Using this transformer makes all request parameters available as variables in the XSL stylesheet. First, you have to define the transformer in the components section of your sitemap:

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

Using the Transformer

Then you can use the transformer in your pipelines:

<map:match pattern="*">
  <map:generate src="http://host.com/{1}.xml"/>
  <map:transform type="xslt-with-parameters" src="stylesheet.xsl"/>
  <map:serialize/>
</map:match>

To simplify the usage of the transformer, you can set the use-request-parameters parameter directly in the sitemap:

<map:match pattern="*">
  <map:generate src="http://host.com/{1}.xml"/>
  <map:transform type="xslt" src="stylesheet.xsl">
    <map:parameter name="use-request-parameters" value="true"/>
  </map:transform>
  <map:serialize/>
</map:match>

Using the Parameters in Your XSLT Stylesheet

In your XSLT stylesheet, you first have to define the parameters you want to access:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'
  xmlns:xlink="http://www.w3.org/1999/xlink">

...
<xsl:param name="page-title"/>
...

Then you can use the value of your parameter:

<xsl:template match="document">
  <html>
    <head>
      <title><xsl:value-of select="$page-title"/></title>
    </head>

    ...
  </html>
</xsl:template>