This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: xsl:script


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
msdn.microsoft.com, 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"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:foo="http://www.broadbase.com/foo"
                extension-element-prefixes="msxsl"
                exclude-result-prefixes="foo">

<!-- 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"
              implements-prefix="foo">
              
   <!-- it's often helpful to use a CDATA section so you don't have to
        worry about escaping less-than signs or anything -->
   <![CDATA[

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

   ]]>
</msxsl:script>

<xsl:template match="/">
   <p>
      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()" />.
   </p>
</xsl:template>
                
</xsl:stylesheet>

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:

<foo:months>
   <foo:month>January</foo:month>
   <foo:month>February</foo:month>
   <foo:month>March</foo:month>
   ...
</foo:months>

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

<xsl:value-of
  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">
  <month>January</month>
  <month>February</month>
  <month>March</month>
  ...
</xsl:variable>

and retrieving it with an MSXML-specific extension function, i.e.
msxsl:node-set:

<xsl:value-of
  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

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



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]