XML
  Home arrow XML arrow Page 4 - Using Script Extensions in Xalan-Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Using Script Extensions in Xalan-Java
By: Seamus Donohue
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2004-06-16

    Table of Contents:
  • Using Script Extensions in Xalan-Java
  • Xalan-Java Scripting Extensions, Resources, Requirements
  • The Scripting Scenario
  • Plugging the Scripts into the XSLT Stylesheet
  • Python
  • VBScript
  • PerlScript
  • Running the Stylesheet

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Script Extensions in Xalan-Java - Plugging the Scripts into the XSLT Stylesheet


    (Page 4 of 8 )

    Xalan enables script files to be imported from an external file or inserted in-line into the stylesheet directly. In the examples in this article, all scripts are stored in-line. Xalan supports multiple script types in the same stylesheet. However, each script type must be placed in a separate <component> element. A sample JavaScript component is shown below:

    <lxslt:component prefix="javascript" elements="" functions="calculate_surname, calculate_firstname, calculate_bonus">

    Function X declared in this script is then accessed via the XSLT fragment:

    <xsl:value-of select="javascript:X( param1, param2, … )"/>

    Integrating and using Java-based script interpreters (for example, JavaScript and Jython) is relatively straightforward. Integrating Windows ActiveScript script support such as VBScript, Jscript, and PerlScript requires some additional configuration; JavaBean access is also slightly different with ActiveScript.

    The following sections describe how JavaScript, Python, VBScript, and PerlScript are used to implement the scripting scenario.

    JavaScript

    The JavaScript support is based on Rhino version 1.5.

    Configuration

    Ensure that the js.jar file is in the classpath used by the XSLT processor.

    Simple Function without a JavaBean Parameter

    The function below shows how JavaScript regular expression support is used to calculate the output surname element using the input name element:

    function calculate_surname( name )
    {
     return name.split( /s/g )[1];  
    }

    The XSLT function call is:

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

    Note that the name element is coerced into a string type before it is passed to the function. This will ensure that a string type rather than a node type is passed to the function.

    Function with JavaBean Processing

    The function below shows how JavaScript handles JavaBean parameters. This function must perform a table lookup in order to calculate the bonus awarded to a salesperson. Because the JavaScript interpreter is Java-based, the bean instance is passed directly to the script. This is not possible for script interpreters that are not Java-based, as we will see later when we discuss using Windows ActiveScript, which is based on C and C++.

    function calculate_bonus( quota, actual, bonusplan, bonus_table )
    {
     /* find base bonus from lookup table */
           var bonus_base = parseFloat( bonus_table.lookUp(bonusplan,1), 10 ) ;
                   
          /*calculate multiple, bonus multiple is zero if they underperform */
           var multiple = (actual > quota) ? actual/quota : 0 ;
                   
          return bonus_base * multiple;
    }

    The bonus_table.lookup operation returns a string that must be explicitly converted to float, because JavaScript is a strongly typed language. The rest of the script calculates the sales success (the multiple value) and returns the bonus award calculation as the bonus_base (from the table) multiplied by multiple.

    The XSLT function call is as follows:

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

    Again, note that parameters are converted to XSLT number and string types, where required, before they are passed to the JavaScript (the XSLT number type is mapped to a Java double type). This is good practice, because it will keep strongly typed scripts from having to coerce values to the correct type before processing them. The bonus_table variable is passed as is.

    More XML Articles
    More By Seamus Donohue


     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT