EXSLT - func:function - Implementer Page

Version: 3
Status: stable
User Page: index.html
XML Definition: func.function.xml
Element Package: func.function.zip

Element Syntax

<func:function
   name = QName>
   <-- Content: (xsl:param* | template) -->
</func:function>

The func:function element can only occur at the top level of the stylesheet. The func:function element declares an extension function that is visible everywhere: the extension function is added to the function library available to the expressions and patterns used in the XSLT stylesheet.

The EXSLT - Functions namespace (http://exslt.org/functions) is designated as an extension namespace within the subtree rooted at a func:function element. The effect of this is as if the func:function element had a xsl:extension-element-prefixes attribute defined on it, with one of the values within it being the prefix used for the EXSLT - Functions namespace (see [14.1 Extension Elements] in [XSLT]).

An func:function element must have a name attribute, indicating the name of the function. The value of the name attribute is a QName, which is expanded as described in [2.4 Qualified Names] in [XSLT]. It is an error if the namespace URI of the expanded name of the function is null - extension functions must not be in a null namespace.

Note: the rules on resolving qualified names entail that if no prefix is defined, the namespace URI resolves to the null namespace. Thus, it is an error if the qualified name specified does not have a prefix.

If a stylesheet contains more than one func:function element with the same name, then the XSLT processor must use the function definition with the highest import precedence. It is an error if a stylesheet contains more than one func:function element with the same name and the same import precedence. An XSLT processor may signal the error; if it does not signal the error, it must recover by using the function definition that occurs last in the stylesheet.

When an extension function defined with func:function is called, the content of the func:function is instantiated to give the result of the function (see [func:result]).

Defining Function Arguments

Arguments for functions are defined with the xsl:param element, as specified in [11. Variables and Parameters] of [XSLT].

When an extension function is called, the values passed as arguments are assigned to parameters according to the position of the xsl:param. The first argument is assigned to the first parameter, the second to the second and so on. The presence of an xsl:param indicates that an argument is expected for the function but does not imply that an argument has to be passed to the function.

An XSLT processor must not signal an error if an extension function is called with fewer arguments than there are parameters defined for the extension function. It is an error to call a function with more arguments than there are parameters defined for the extension function. An XSLT processor may signal the error; if it does not signal the error, then it must recover by ignoring the extra arguments.

As an example, take the following function definition:

<func:function name="my:func">
   <xsl:param name="foo" />
   <xsl:param name="bar" select="false()" />
   ...
</func:function>

All the following function calls are legal:

my:func()
my:func('Fred')
my:func('Fred', true())
my:func('Fred', 'Barney')

The following function call is illegal:

my:func('Fred', true(), 'Barney')

The value specified by an xsl:param indicates the default value for an argument if that argument is not given in a function call, but does not indicate the acceptable value types for the function.

The type of the value passed into the function may be tested with the exsl:object-type function if it is supported.

Function Results

The content of the func:function element is a template. When the function is called, the template is instantiated to give the result of the function. The template is instantiated with the context node from the expression in which the function was called as the current node, and with the context node list from the expression in which the function was called as the current node list.

It is an error if the instantiation of the template results in the generation of result nodes. For example a call to my:func as below is an error.

<func:function name="my:func">
   <foo />
</func:function>

The instantiation of the content of the func:function element may involve the instantiation of an func:result element to enable functions to return node sets (and booleans).

If no func:result element is instantiated, then the result of the function is an empty string.

An implementation of this extension element in the EXSLT func namespace must conform to the behaviour described in this document.

Implementations

Built-in support for func:function is available in the following XSLT processors:

ProcessorProcessor VersionImplemented Version
4XSLT, from 4Suite.0.12.0a33
SAXON 6.3 from Michael Kay6.33
libxslt from Daniel Veillard; implementation by Thomas Broyer1.0.193
Xalan-J from Apache2.4.D13

There are currently no third-party implementations of func:function.

Use Cases

Use Case Package: func.function.use-cases.zip

The following use cases illustrate the functionality of func:function.

DetailsCalling StylesheetXML DataExpected ResultCategoryTemplate
1func.function.1.xslfunc.function.data.1.xmlfunc.function.result.1.xmlexampleN
2func.function.2.xslfunc.function.data.2.xmlfunc.function.result.2.xmlexampleN
3func.function.3.xslfunc.function.data.3.xmlfunc.function.result.3.xmlexampleN
4func.function.4.xslfunc.function.data.4.xmlfunc.function.result.4.xmlexampleN
5func.function.5.xslfunc.function.data.5.xmlfunc.function.result.5.xmlexampleN
6func.function.6.xslfunc.function.data.6.xmlfunc.function.result.6.xmlexampleN

Change History

Previous Version: func.function.2.html

VersionModifiedByDetails
32001-03-31Jeni Tennison

Stopped functions from being able to return result tree fragments just be creating nodes within the content as it increased complexity fairly significantly for not a lot of benefit. Function results now have to be set by func:result.

3.12001-04-28Jeni Tennison
  • Made explicit that if there two function definitions with the same name then the one with the highest import precedence is used.
  • Added implicit designation of the EXSLT - Functions namespace as an extension namespace, effective within the subtree rooted at a func:function element. This to avoid people forgetting to declare the EXSLT - Functions namespace as an extension namespace, and thus having func:result be treated as a literal result element.
3.22001-05-22Jeni Tennison
  • Added vendor implementations of Saxon and 4XSLT
  • Changed status to stable
3.32001-08-15Jeni Tennison

Added vendor implementation in libxslt.

3.42002-08-20Craig Stewart

Added Xalan-J implementation to the list.

3.52002-11-12Craig Stewart

Updated 4XSLT version to 0.12.0a3.

Copyright

http://www.exslt.org/func/elements/function/func.function.html last modified 2002-11-12