XML technologies / XSLT / XSLT and XPath functions / XPath and XSLT functions in general
XPath and XSLT functions in general
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5.)
Various functions can be used in XPath expressions within an XSLT stylesheet for counting, calculations, string inspection and manipulation, node information and more. A call accepts a defined number of arguments and returns its result to the expression containing it.
Formal distinction between XPath and XSLT functions
Formally, functions defined by the XPath/XQuery specification must be distinguished from those defined in XSLT.
Their availability in XSLT stylesheets requires no distinction. However, when an XPath expression is evaluated outside XSLT, for example in a pure XQuery application, only the so-called XPath core functions defined within XPath are available.
Extension functions
Vendors may supply processor-specific extension functions. Each function must have a QName (qualified name), with a namespace prefix bound to a non-empty namespace URI. This prevents collisions with present or future XPath function names.
A widely used, largely processor-independent extension collection, supported by many processors once the relevant extension package is installed, is provided by EXSLT. These free, open-source extensions are organized into modules for mathematics, node sets, dates and other areas.
Many EXSLT functions became part of the XPath 2.0/XQuery 1.0 standard. Extensions can provide often identical functionality in an extended XSLT 1.0 environment.
Examples:
saxon:evaluate() is an extension function provided by the XSLT processor Saxon. Its prefix saxon must be bound to namespace URI "http://saxon.sf.net/".
EXSLT extension function math:min() belongs to the EXSLT Math module. Its prefix math is bound to URI "http://exslt.org/math". Other EXSLT modules have different prefixes and namespaces.
Stylesheet functions
From XSLT 2.0, stylesheet authors can define their own functions using declaration xsl:function. These are usable in XPath expressions during stylesheet execution and are therefore called stylesheet functions.
Their QNames must also have a namespace prefix bound to a non-empty URI. Provide fallback options (backward-compatible routines usable instead of the stylesheet function) so older processors can process the stylesheet without errors.
Stylesheet functions instead of extension functions
A stylesheet function can reproduce an extension function. Giving both the same qualified name makes the stylesheet theoretically usable outside the extended environment, provided stylesheet functions are supported. The stylesheet function then takes over the extension’s role.
When names coincide, the processor’s extension normally takes precedence. This makes sense because its closer integration generally makes it faster.
The stylesheet declaration can explicitly override a same-named extension. This is needed when replacing an implemented extension with a function having a different number of arguments (arity) or different input types.
Function overloading in XSLT/XPath
Stylesheet functions may share a name if their argument counts differ. The number of supplied parameters identifies the function to call.
Function namespaces
XSLT 1.0 and XSLT 2.0:
Functions defined in either XSLT version automatically belong to the XSL namespace, even without an explicit prefix.XPath 1.0:
XPath 1.0 functions likewise automatically belong to the XSL namespace, even without an explicit prefix.XPath 2.0:
XPath 2.0/XQuery uses a separate function namespace (standard function namespace), normally bound to prefix fn: (namespace declaration follows):
xmlns:fn="http://www.w3.org/2005/xpath-functions"
Calling an XPath/XSLT function
Function calls occur within XPath expressions. Generally, the function name (a QName, possibly with a namespace prefix) is followed by a pair of argument parentheses:
funktionsname()
prefix:funktionsname()
XSLT 2.0 processor Saxon still understands XPath core calls without prefix fn. (Calls with prefix fn are also possible, but Saxon then requires an explicit declaration of the corresponding namespace in the stylesheet.)
In the XSLT 2.0 processor supplied with AltovaXML, the function namespace is also predefined. Thus the programmer may omit prefix fn or use it without an explicit namespace declaration. Future implementations need not follow this behavior.
XSLT function calls require no prefix. Stylesheet and extension functions must have their own namespaces and distinct prefixes and be called accordingly, preventing confusion even between functions with the same local name.
Function-call arguments
Each function specifies the number, order and value types of its arguments:
funktionsname(argument1, argument2)
Some XPath and XSLT functions have several signatures accepting different numbers of parameters. Optional parameters can receive the empty sequence, comparable but not identical to an empty string in XPath 1.0. Passing more arguments than the definition permits certainly causes an error.
Only function fn:concat(), which concatenates any supplied strings into one result string, the number of arguments (arity).
Where necessary, an argument is converted to the required type, such as boolean, string or number. An error is reported only when this is impossible.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn