xsl-list
[Top] [All Lists]

RE: xsl:function

2003-03-27 03:39:28
You can use any namespace you like for your functions, it's just there
to make sure that your functions don't conflict with anyone else's. If
you are writing a function library that will be widely deployed, use a
namesapce URI like http://namespaces.mega.co.jp/xslt/trig-module. If you
are writing something that will be used once and thrown away, and are
feeling lazy, use a namespace URI like "zzzz".

Note that a namespace URI is an identifier, not an address. It's a
unique name, it doesn't "point to" anything.

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com 

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Mac Martine
Sent: 26 March 2003 22:56
To: 'Jeni Tennison'
Cc: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] xsl:function



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(_at_)jenitennison(_dot_)com] 
Sent: Wednesday, March 26, 2003 1:58 PM
To: Mac Martine
Cc: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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


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



<Prev in Thread] Current Thread [Next in Thread>