Subject: Re: [xsl] xsl:function From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Wed, 26 Mar 2003 21:57:57 +0000 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] xsl:function, Mac Martine | Thread | RE: [xsl] xsl:function, Mac Martine |
Re: [xsl] pretty-printing XML, Mike Brown | Date | Re: [xsl] XML to XML, Rick Taylor |
Month |