Re: Function call to set a variable

Subject: Re: Function call to set a variable
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 09 Oct 2000 14:45:10 +0100
Tomas,

>I am trying to set a variable to the value that is returned
>by one of my javascript functions:
>
><xsl:variable name="Language">getLanguage()</xsl:variable>
>
>The example above does not work, but is it possible to call
>my getLanguage() function and set the variable to this value?
>An in that case, how?

There isn't a standard way of incorporating scripted functions into XSLT
1.0.  However, MSXML has an extension element, msxsl:script, that allows
you to do so.  To use it, you have to be using MSXML as your XSLT Processor
(e.g. be doing client-side processing with IE).  Within your stylesheet (on
the xsl:stylesheet element), you have to specify two namespaces: the msxsl
namespace that the extension element belongs to:

  xmlns:msxsl="urn:schemas-microsoft-com:xslt"

and a namespace for your functions, which can be anything you like, but
should be something that you have control over, e.g.:

  xmlns:user="tomas.olsson@xxxxxxxxxxxxx"

This gives a start tag for the xsl:stylesheet element that looks like:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="tomas.olsson@xxxxxxxxxxxxx"
                version="1.0">

You then need to define your functions within an msxsl:script element at
the top level of the stylesheet.  The msxsl:script element takes two
attributes: 'language', which gives the language the functions are written
in (i.e. Javascript), and 'implements-prefix', which gives the prefix for
the namespace you're using (i.e. 'user'):

<msxsl:script language="Javascript" implements-prefix="user">
  function getLanguage() {
    ...
  }
</msxsl:script>

Finaly, in order to set the value of a variable to the result of calling
this function, you need to put the call to the function within the 'select'
attribute of an xsl:variable element, and use the namespace prefix you've
specified to indicate that it's a user-defined function:

<xsl:variable name="Language" select="user:getLanguage()" />

There's more about this in the MSXML SDK.

I hope that this helps,

Jeni

Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread