FilterTransformer

Sometimes it is necessary to restrict the number of tags within a block. This is particular relevant to SQL results which are returned as a set of rows. A typical use of FilterTransformer would be

<map:transformers default="xslt"> <map:transformer name="xslt" src="resource://lib/transforming/TRAXTransformer"> <map:use-request-parameters>true</map:use-request-parameters> </map:transformer> <map:transformer name="mysql" src="resource://lib/transforming/SQLTransformer"> <map:parameter name="type" value="mysql"/> <map:parameter name="host" value="localhost:3306"/> <map:parameter name="user" value="root"/> </map:transformer> <map:transformer name="filter" src="resource://lib/transforming/FilterTransformer"/> </map:transformers> ... <map:pipeline> <map:match pattern="**.html"> <map:generate src="context://content/{1}.xml" label="xml-content"/> <map:transform type="mysql" label="sql-transform"> <map:parameter name="show-nr-of-rows" value="true"/> <map:parameter name="composer" value="Bach"/> </map:transform> <map:transform type="filter"> <map:parameter name="element-name" value="http://apache.org/cocoon/SQL/2.0:row"/> <map:parameter name="count" value="2"/> <map:parameter name="blocknr" value="3"/> </map:transform> ... </map:match>

where

Say the following data is stored in the database:

+------------+------------------+------------+------------+ | name | forenames | birth | death | +------------+------------------+------------+------------+ | Mozart | Wolfgang Amadeus | 1756-01-27 | 1791-12-05 | | Beethoven | Ludvig van | 1770-12-15 | 1827-03-26 | | Bach | Johann Sebastian | 1685-03-21 | 1750-07-28 | | Bach | Johann Christian | 1735-09-05 | 1782-01-01 | | Haydn | Franz Joseph | 1732-03-31 | 1809-05-31 | | Bernstein | Leonard | 1918-08-25 | 1990-10-14 | | Boccherini | Luigi | 1743-02-19 | 1805-05-28 | | Ravel | Joseph Maurice | 1875-03-07 | 1937-12-28 | +------------+------------------+------------+------------+

Then the above filter instance (count=2 and blocknr=3) would return from a query "select * from composer", the following XML:

<page:content xmlns:default="http://apache.org/cocoon/SQL/2.0" xmlns:t="http://www.hsfr.org.uk/Schema/Text"> <t:heading level="1">SQL Transform Test</t:heading> <default:row-set nrofrows="8" name="music"> <default:block id="1"/> <default:block id="2"/> <default:block id="3"> <default:row> <default:name>Haydn</default:name> <default:forenames>Franz Joseph</default:forenames> <default:birth>1732-03-31</default:birth> <default:death>1809-05-31</default:death> </default:row> <default:row> <default:name>Bernstein</default:name> <default:forenames>Leonard</default:forenames> <default:birth>1918-08-25</default:birth> <default:death>1990-10-14</default:death> </default:row> </default:block> <default:block id="4"/> </default:row-set> </page:content>
Copyright 2006 – 2023 Hugh Field-Richards. All Rights Reserved.