This article will look at scripting languages that are not covered in the Xalan documentation, in particular, Python, VBScript, and PerlScript. In addition, we will show how Java Object instances, created in XSLT as part of the Java language extensions mechanism, are passed to these scripts and utilized by them. These samples will provide a solid foundation on which to build more complicated script-based extensions.
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:
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.