Search code examples
apache-cocoon

How to read a flowscript value when generating an XML for processing in Apache Cocoon 2.2


I have a simple flowscript function that will do this:

cocoon.sendPage("page/index",
  {
    username : "SomeName"
  }
);

In my sitemap.xmap I have this configuration:

<map:pipeline internal-only="true">
  <map:match pattern="page/*">
    <map:generate src="xml/{1}.xml"/>
    <map:transform src="xslt/html.xslt"/>
    <map:serialize type="html"/>
  </map:match>
</map:pipeline>

I'm using xsl:stylesheet in the html.xslt file to read values from the page.xml-file. It's simple and straight forward (and works as expected).

However: I want to read flowscript values (here: username) in the page.xml-file in order to pass it on to the html.xslt-file. Can this be done by jx:template? (Examples I've found uses that, but they don't work well in Apache Cocoon 2.2, only earlier versions..) If not jx:template's the solution: what else is there?


Solution

  • To read values from flowscript, you can use the JX Template Generator. But unlike Cocoon 2.1 and earlier versions you shouldn't have an explicit reference to org.apache.cocoon.generation.JXTemplateGenerator in the map:generator-section of your sitemap.

    For future reference, here's the solution (using Apache Cocoon 2.2 and Maven 3.0.4):

    1) Make sure your pom.xml has a dependency to cocoon-template-impl (the JXTemplateGenerator has been moved here)

    <dependency>
       <groupId>org.apache.cocoon</groupId>
       <artifactId>cocoon-template-impl</artifactId>
       <version>1.1.0</version>
     </dependency>
    

    Verify that your Maven repository contains the library. If not: install it.

    2) In flowscript: have a function that will set some values and make a call to .SendPage()/.SendPageAndWait()

    cocoon.sendPage("page/index",
      {
        username : "SomeName"
      }
    );
    

    3) In sitemap.xmap: when generating XML-files, make sure they have type="jx"

    <map:pipeline internal-only="true">
      <map:match pattern="page/*">
        <map:generate type="jx" src="xml/{1}.jx.xml"/>
        <map:transform src="xslt/page.xslt"/>
        <map:serialize type="html"/>
      </map:match>
    </map:pipeline>
    

    4) In your .jx.xml-file: read values like this

    <?xml version="1.0" encoding="UTF-8"?>
    <jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
    <valuesFromFlowscript>
      <!-- Select values from flowscript -->
      <username>${username}</username>
    </valuesFromFlowscript>
    </jx:template>
    

    5) In your .xslt-file you can read the generated value like this

    <xsl:value-of select="valuesFromFlowscript/username"/>