[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: xsl:function

Subject: RE: xsl:function
From: "Mac Martine" <email@xxxxxxxxxxxxxxxx>
Date: Wed, 26 Mar 2003 14:55:52 -0800
xsl function example
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: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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.