Home arrow XML arrow Page 7 - 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 - PerlScript
(Page 7 of 8 )

PerlScript is based on the PerlScript interpreter provided by http://www.activestate.com.


This requires the exact same configuration as that required for VBScript. In addition, the ActiveState Perl interpreter must be installed on the same machine as the stylesheet processor.

Simple Function without a JavaBean Parameter

The function below shows how PerlScript is used to calculate the surname output element using the name input element value.

sub calculate_surname
 my ( $name ) = @_;               
       @tokens = split(" ",$name);
       return @tokens[1];

The first line is not strictly necessary, but copying the parameters from the standard @_ parameters array into a list of named parameters makes the script more readable.

The XSLT function call is:

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

Function with JavaBean Processing

The function below shows how VBScript is used to calculate the bonus value. Again, as this is an ActiveScript, it requires the same workaround as before when passing JavaBean instances.

 sub calculate_bonus
 my ( $quota, $actual, $bonusplan, $bonus_table_bean_name ) = @_;                    
 # set up lookup table variables
       $bonus_table = $bsf->lookupBean( $bonus_table_bean_name );
 # find base bonus from lookup table
 $bonus_base = $bonus_table->lookUp($bonusplan,1) ;
       # calculate multiple, bonus multiple is zero if they underperform                   
       $multiple = $actual > $quota ? $actual / $quota : 0.0 ;
       return $bonus_base * $multiple;               

Because PerlScript 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="perlscript:calculate_bonus( number(quota), number(actual), string(bonusplan), $bonus_table )"/>

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