September 4, 2008

Use Saxon in Cocoon 2.2

Saxon is a great XSLT engine and supports XSLT 2.0. I normally use it for almost of all my transformations. But getting it to work in Cocoon 2.2 needs some steps. We will see which steps are necessary to get Saxon working in a Cocoon 2.2 block.

We first must have a Cocoon block to which we will add Saxon support. Next we create the directory src/main/resources/META-INF/cocoon/avalon. In this directory we can add component definitions, which are used by our block. In this directory we add the file cocoon-core-xslt-saxon.xconf. The contents of the file contains the Saxon transformer definition:

<?xml version="1.0" encoding="UTF-8"?>
    <component role="org.apache.excalibur.xml.xslt.XSLTProcessor/saxon"
        <parameter name="use-store" value="true"/>
        <parameter name="transformer-factory" value="net.sf.saxon.TransformerFactoryImpl"/>

Notice we use /saxon at the end of value for the role attribute and we need this name for further configuration. We now create another file in the src/main/resources/META-INF/cocoon/avalon directory with the name sitemap-transformers-saxon-transformer.xconf. In the file we add a transformer definition to reference the Saxon transformer component. Notice the xslt-processor-role must be saxon like we defined in our component definition:

<map:components xmlns:map="http://apache.org/cocoon/sitemap/1.0">
  <map:transformer name="saxon" src="org.apache.cocoon.transformation.TraxTransformer">

Now we only need to a dependency to our pom.xml to include the Saxon library in our application classpath:


And now we can use the Saxon transformation in our Cocoon 2.2 block.