Re: [xsl] xsl:script

Subject: Re: [xsl] xsl:script
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 19 Jan 2001 19:29:01 +0000
Hi Shimon,

Just in case you haven't got it yet, what David C.'s getting at is
that you can mix namespaces to use the MSXML 'msxsl:script' extension
element *as well as* all the really cool stuff you get with XSLT 1.0.

As David says, the MSXML3.0 SDK documentation, which you can get from, tells you about what you can do with msxsl:script.
But here's a small example.

<!-- include in the xsl:stylesheet element:
     (a) the version attribute as usual
     (b) the XSLT namespace declaration as usual
     (c) the MSXSL namespace declaration
     (d) a namespace declaration to identify your functions
     (e) the 'extension-element-prefixes' attribute to give the
         namespace prefixes that indicate extension elements
         (i.e. 'msxsl')
     (f) the 'exclude-result-prefixes' attribute to indicate the
         namespaces that aren't supposed to be part of the result
         tree (i.e. 'foo') -->
<xsl:stylesheet version="1.0"

<!-- do whatever output you want - you can use full XSLT functionality
<xsl:output method="html" />

<!-- define the Javascript functions that you want to include within
     a msxsl:script element.
     - language indicates the scripting language
     - implements-prefix gives the namespace prefix that you declared
       for your function (i.e. foo) -->
<msxsl:script language="javascript"
   <!-- it's often helpful to use a CDATA section so you don't have to
        worry about escaping less-than signs or anything -->

   <!-- define your functions -->
   function today() {
      today = new Date();
      return today.toLocaleString();


<xsl:template match="/">
      This page brought to you on
      <!-- call your functions using the prefix that you've used (i.e.
           foo) anywhere you can normally use an XPath function, but
           make sure it's returning the right kind of object -->
      <xsl:value-of select="foo:today()" />.

When XSLT 1.1 comes along, it's fairly likely that you'll just be able
to do a global search & replace on 'msxsl:script' for 'xsl:script' and
be XSLT 1.1 compliant.

> I have to write simple functions in JavaScript like obtaining month
> name from the number of the month,

Since you mention this: the classic way of doing this in XSLT is to
add some XML defining the months within the stylesheet itself:


You then get the name of the month by accessing this XML using the
document() function:

  select="document('')/*/foo:months/foo:month[$month-number]" />

As you are using MSXML-specific code anyway, you could alternatively
do this by defining a variable:

<xsl:variable name="months">

and retrieving it with an MSXML-specific extension function, i.e.

  select="msxsl:node-set($months)/month[$month-number]" />

I mention this to compare and contrast with msxsl:script: both are
extensions over and above the functionality of XSLT 1.0, msxsl:script
an extension element and msxsl:node-set() an extension function. To
use them you just have to use a processor that understands them,
declare the relevant namespace, and specify that it's an extension
namespace, and you're all set.
I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread