RE: [xsl] xsl:function

Subject: RE: [xsl] xsl:function
From: "Mac Martine" <email@xxxxxxxxxxxxxxxx>
Date: Wed, 26 Mar 2003 14:55:52 -0800
Great, thanks. So, I think <func:function> will work, so I'm trying to
use that. I'm now just confused as to where the namespace declaration
should point. You use "my:", but I don't know how I know where xmlns:my
should point to. My function is just going to return true or false after
string matching. According to everything I see on the web this is
considered common knowledge, so noone seems to explain this... or I'm
just overlooking something super simple. Anyway...
Thanks-
 Mac


-----Original Message-----
From: Jeni Tennison [mailto:jeni@xxxxxxxxxxxxxxxx] 
Sent: Wednesday, March 26, 2003 1:58 PM
To: Mac Martine
Cc: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] xsl:function

Hi Mac,

> Would someone please give me a simple example of creating a user
> defined function using <xsl:function>
>
> I'm having a really hard time finding complete examples for some
> reason.

I suspect that's because <xsl:function> was only introduced in XSLT
2.0, which isn't even a Last Call Working Draft yet and has very few
implementations.

<xsl:function> works in roughly the same way as <func:function> as
defined in EXSLT (http://www.exslt.org/func/elements/function). You
can find lots of examples of <func:function> on the EXSLT site -- most
of the functions defined there have a <func:function> implementation.

An example is the following fairly useless function that adds two
things together:

<xsl:function name="my:add">
  <xsl:param name="val1" />
  <xsl:param name="val2" />
  <xsl:result select="$val1 + $val2" />
</xsl:function>

All functions you define with <xsl:function> have to be in some
namespace, which means that their names are always qualified. In this
example, you have to have the 'my' prefix associated with a namespace
at the top of your stylesheet.

You can call the function with, for example:

  <xsl:value-of select="my:add(1, 3)" />

to get the value 4.
  
If you want, you can constrain the types of the parameters to the
function and declare the type of the result using 'as' attributes.
This will enable/force the implementation to raise type errors if the
function is passed the wrong type of arguments or used somewhere that
expects something other than a number. For example, to create a
my:add() function that will only work with integers:

<xsl:function name="my:add">
  <xsl:param name="val1" as="xs:integer" />
  <xsl:param name="val2" as="xs:integer" />
  <xsl:result select="$val1 + $val2" as="xs:integer" />
</xsl:function>

Note again that the 'xs' prefix has to be associated with the
'http://www.w3.org/2001/XMLSchema' namespace at the top of your
stylesheet.

If you're after concrete examples of user-defined functions in use, I
used quite a few in some stylesheets I wrote over the weekend, which
are available at:

  http://www.lmnl.org/projects/LMNLCreator/LMNLCreator.xsl
  http://www.lmnl.org/projects/LMNLSchema/LMNLNester.xsl

The stylesheets are not run-of-the-mill, but they do use XSLT 2.0
features, including <xsl:function>, quite heavily.

Cheers,

Jeni

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



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


Current Thread