Jython

We’ll start our tour of BSF-supported languages with Jython, an implementation of Python written in Java. As you would expect, we must do several things to identify our extension code to Xalan. We’ll cover them, and then look at the source of the extension functions. First we need to define the namespace prefixes we’ll use:

<!-- piechart-jython.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:months="http://www.oreilly.com/xslt/months"
  xmlns:lxslt="http://xml.apache.org/xslt"
  xmlns:jython-extension="http://www.jython.org"
  extension-element-prefixes="jython-extension"
  exclude-result-prefixes="lxslt">

We’re generating SVG markup, so we need to define the svg namespace. The months namespace is for the <months> elements we use for a document('') lookup table (for variety’s sake, we’ll avoid using the EXSLT extensions here). For the last two namespaces, lxslt is the namespace Xalan uses to invoke the Bean Scripting Framework, and jython-extension is the prefix we use to identify the extension functions written in Jython.

Tip

In this example, we associated the jython-extension namespace prefix with the URI http://www.jython.org, which is the home page for the Jython project. The URI could be anything. The crucial identifier for the BSF is the name of the language, as we’ll see in just a minute.

Our code goes in a <lxslt:component> element:

 <lxslt:component prefix="jython-extension" functions="cos ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.