Using Script Extensions in Xalan-Java
(Page 1 of 8 )
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.
Find support files for this article here.
Why Use Scripting in XSLT? Extensible Stylesheet Language Transformations (XSLT) is an ideal language for building transformations that convert a group of XML elements into another structure. However, when it comes to transforming element values, it may be more appropriate to use different scripting languages. For example, let’s say we want to transform the following document:
<report>
<sales>100</sales>
<plan>gold</plan>
</report>
into this one:
<commission>75</commission>
where the commission element value is calculated as follows:
if plan is silver then commission = 50% of sales
else if plan is gold then commission = 75% of sales
The XSLT author may choose to implement this rather simple transformation algorithm in JavaScript as:
if (bonusplan.equals('silver'))
return actual * 0.50;
else if (bonusplan.equals('gold'))
return actual * 0.75;
else return 0;
rather than in XSLT as:
<xsl:choose>
<xsl:when test="bonusplan = 'silver'">
<xsl:value-of select="actual * 0.50"/>
</xsl:when>
<xsl:when test="bonusplan = 'gold'">
<xsl:value-of select="actual * 0.75"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0"/>
</xsl:otherwise>
</xsl:choose>
Switching from XSLT to another scripting language makes sense when:
- The other scripting language is easier to write than XSLT. For example, if the scripting language supports facilities that XSLT does not such as native regular expressions or easy database access features.
- The script exists already - there is no need to port the transformation to XSLT!
- The programmer is already very proficient in that scripting language.
This article explores how programmers can fully exploit XSLT scripting extension support in Xalan-Java.
Next: Xalan-Java Scripting Extensions, Resources, Requirements >>
More XML Articles
More By Seamus Donohue