JRuby

JRuby is an implementation of the popular Ruby language written in Java. We define the namespace prefix jruby-extension and associate it with the URI http://jruby.codehaus.org. Here’s the JRuby code:

<?xml version="1.0"?>
<!-- piechart-jruby.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:jruby-extension="http://jruby.codehaus.org"
  extension-element-prefixes="jruby-extension"
  exclude-result-prefixes="lxslt">
...
  <lxslt:component prefix="jruby-extension" 
    functions="cos sin">
    <lxslt:script lang="ruby">
      def cos(d)
        Math::cos(d)
      end

      def sin(d)
        Math::sin(d)
      end
    </lxslt:script>
  </lxslt:component>

To use these extension functions, your CLASSPATH must contain jruby.jar, available at http://jruby.codehaus.org, in addition to bsf.jar and commons-logging-1.1.jar.

Tip

The magic lang attribute value here is ruby. Using JRuby, jruby, or Ruby, all of which are reasonable choices, causes a runtime error.

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.