JavaScript

We’ll use Mozilla’s Rhino JavaScript engine to illustrate JavaScript support. We define the namespace prefix javascript-extension and associate it with the URI http://www.mozilla.org/rhino. The JavaScript code looks like this:

<?xml version="1.0"?>
<!-- piechart-javascript.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:javascript-extension="http://www.mozilla.org/rhino"
  extension-element-prefixes="javascript-extension"
  exclude-result-prefixes="lxslt">
...
  <lxslt:component prefix="javascript-extension" 
    functions="cos sin">
    <lxslt:script lang="javascript">
      function cos(d)
      { 
        return Math.cos(d); 
      }

      function sin(d)
      { 
        return Math.sin(d); 
      }
    </lxslt:script>
  </lxslt:component>

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

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.