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.
The VBScript interpreter is implemented in extension DLLs. The required DLLs are shipped with the BSF 2.2 binary installation in the <BSF 2.2 home dir>libwin32 directory.
Ensure that the following JVM parameter is set on the XSLT processor JVM:
-Djava.library.path=<BSF 2.2 home dir>/lib/win32
Simple Function without a JavaBean Parameter
The function below shows how VBScript is used to calculate the surname output element using the name input element value.
Function calculate_surname( Name ) Dim Tokens Tokens = Split( Name ) calculate_surname = RTrim(Tokens(1)) End Function
Even though VBScript is a loosely typed scripting language, it is still very important to coerce script function parameters to the appropriate XSLT type. The BSF ActiveScript engine will pass simple types directly to the VBScript by invoking toString() operations on the parameter type (strings are a special case, because they are also enclosed in quotes). Any type which is not a simple type or a string is treated as a bean and dealt with differently (as discussed below).
Function with JavaBean Processing
Function calculate_bonus(Quota, Actual, Bonusplan, Bonus_Table_Name) ' retrieve the bean from bsf Set Bonus_Table = bsf.lookupBean( CStr(Bonus_Table_Name) )
' find base bonus from lookup table Bonus_Base = Bonus_Table.lookUp(Bonusplan,1)
' calculate multiple, bonus multiple is zero if they underperform If Actual > Quota Then Multiple = Actual / Quota calculate_bonus = Bonus_Base * Multiple Else calculate_bonus = 0.0 End If End Function
Because VBScript is loosely typed, it is not necessary to convert the string retrieved from the lookup table into a float.
Again, there is no difference in the XSLT function call except for the namespace. The bonus_table variable is passed as is; however, the corresponding parameter in the function declaration for this parameter will be the BSF-declared bean name, as opposed to the bean itself (as explained above).