Home arrow XML arrow Page 6 - Using Script Extensions in Xalan-Java

Using Script Extensions in Xalan-Java

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.

Author Info:
By: Seamus Donohue
Rating: 5 stars5 stars5 stars5 stars5 stars / 14
June 16, 2004
  1. · Using Script Extensions in Xalan-Java
  2. · Xalan-Java Scripting Extensions, Resources, Requirements
  3. · The Scripting Scenario
  4. · Plugging the Scripts into the XSLT Stylesheet
  5. · Python
  6. · VBScript
  7. · PerlScript
  8. · Running the Stylesheet

print this article

Using Script Extensions in Xalan-Java - VBScript
(Page 6 of 8 )

VBScript is based on ActiveScript support on Windows platforms. For more information, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vbswhat.asp.


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

Unlike the two previous examples (JavaScript and Jython), the patched bsf.jar file (see the appendix for details) is required to use VBScript (or any other ActiveScript, such as JScript or PerlScript). The patched bsf.jar file (not the bsf.jar file that is shipped with Xalan) must be placed in the XSLT processor classpath.

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

The XSLT function call is:

<xsl:value-of select="vbscript:calculate_surname( string(name) )"/>

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

The function below shows how VBScript is used to calculate the bonus value. Note that VBScript treats the JavaBean parameter differently from Python or JavaScript. Because the ActiveScript engine is not implemented in Java, it is not possible to pass the JavaBean parameter directly to the script. Instead the patched bsf.jar file declares the bean parameter to the Bean Scripting Framework (BSF). The JavaBean parameter is then substituted with the declared bean name. The script must then retrieve the JavaBean from the BSF using this name. This layer of indirection introduces a slight complication, but without this mechanism, it would not be possible to pass the JavaBean parameter as is.

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
     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.

The XSLT function call is:

<xsl:value-of select="vbscript:calculate_bonus( number(quota), number(actual), string(bonusplan), $bonus_table )"/>

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).

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials