Using a RequestSelector

by Andreas Hartmann

Another way to access request parameters in the sitemap is using a RequestSelector.

Some advantages over a RequestParamAction are that

  • you can use a default stylesheet when the parameter is not present,
  • you can change the stylesheet filenames without changing the request parameter values.

Defining the Selector

First, you have to define the selector in the components section of the sitemap:

<map:components>
  ...
  <map:selectors default="request">
    <map:selector name="request"
      src="org.apache.cocoon.selection.RequestSelector"/>
  </map:selectors>
  ...
</map:components>

Using the selector

A possible use of the selector is selecting the XSLT stylesheet based on a certain request parameter. When you pass the parameter "style=fancy" in the URI, the stylesheet fancy-style.xsl is used to transform the XML source. If you use another value for this parameter, or omit it, the stylesheet default-style.xsl is used.

<map:match pattern="*">
  <map:generate src="http://host.com/{1}.xml"/>
  <map:select type="request">
    <map:parameter name="parameter-name" value="style"/>
    <map:when test="fancy">
      <map:transform src="fancy-style.xsl"/>
    </map:when>
    <map:otherwise>
      <map:transform src="default-style.xsl"/>
    </map:otherwise>
  </map:select>
  <map:serialize/>
</map:match>