Using Script Extensions in Xalan-Java - The Scripting Scenario
(Page 3 of 8 )
In this example, we want to convert the following sales report document:
<ns0:File xmlns:ns0='http://www.your-company.com/Untitled.xsd'>
<record>
<name>Billybob Sellemfast</name>
<quota>3000</quota>
<actual>7000</actual>
<bonusplan>gold</bonusplan>
</record>
<record>
<name>Rick Sellemslow</name>
<quota>1000</quota>
<actual>1200</actual>
<bonusplan>silver</bonusplan>
</record>
<record>
<name>Chad Selemnever</name>
<quota>2000</quota>
<actual>1000</actual>
<bonusplan>gold</bonusplan>
</record>
</ns0:File>
to the sales bonus report document shown below:
<ns0:File xmlns:ns0='http://www.your-company.com/Untitled.xsd'>
<record>
<surname>Sellemfast</surname>
<firstname>Bob</firstname>
<bonus>1245</bonus>
</record>
<record>
<surname>Sellemslow</surname>
<firstname>Bob</firstname>
<bonus>123</bonus>
</record>
<record>
<surname>Selemnever</surname>
<firstname>Bob</firstname>
<bonus>0</bonus>
</record>
</ns0:File>
For each record in the input document, a record is created in the output document. The surname and firstname element values in the output document are created from the name element in the input document. The bonus element value in the output document is created using the quota, actual, and bonusplan element values in the input document, as well as a special bonus plan lookup table. In each case, the value of the output document elements surname, firstname, and bonus are all calculated using scripts. These scripts are discussed in detail in subsequent sections.
The lookup table is implemented as a Java Class instance. In the XSLT file, it is specified as a global bonus_table variable, instantiated in the XSLT fragment shown below:
<xsl:variable name="bonus_table" select="cc-xslt-extensions:LookupTable.new( 'bonus_plan_table.txt','{0}%{1}',0)"/>
This LookupTable constructor takes three parameters: the table data file name, the file format specified as a Java format string, and the primary key column number. The contents of the data file used in this example are shown below:
gold%10000
silver%5000
bronze%2000
Once this Java instance is created, the table is parsed using the Java format string, loaded into a hashed map, and hashed by the first column value (the bonus plan name). Where a script requires table lookups as part of a calculation, the Java instance is passed to the script as an additional parameter. The BSF ensures that this Java Class instance is accessible from the script. In later examples we will show how various scripts use Java Class instances passed as parameters to the script function.
Next: Plugging the Scripts into the XSLT Stylesheet >>
More XML Articles
More By Seamus Donohue