xsl:function
Defines a custom XPath function callable from any XPath expression in the stylesheet.
<xsl:function name="my:double" as="xs:integer"
xmlns:my="http://example.com/my">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="$n * 2"/>
</xsl:function>The function name must be a QName in a non-XSLT, non-built-in namespace. Each xsl:param declares one positional parameter; functions don’t have default values, so all parameters are required. The body is a sequence constructor whose result is returned. Use xsl:sequence (rather than xsl:value-of) to return typed values rather than text nodes.
Functions can be overloaded by arity: two xsl:function declarations with the same name but a different number of parameters are distinct functions.
Attributes
| Attribute | Description |
|---|---|
name |
QName of the function. Required. |
as |
Sequence type of the return value. |
override-extension-function |
yes or no. |
visibility |
Visibility for package mode (public, private, …). |
Examples
Recursive factorial:
<xsl:function name="my:fact" as="xs:integer"
xmlns:my="http://example.com/my">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="
if ($n <= 1) then 1
else $n * my:fact($n - 1)"/>
</xsl:function>
<!-- Usage: -->
<xsl:value-of select="my:fact(5)"/> <!-- 120 -->Two arities:
<xsl:function name="my:greet">
<xsl:param name="who"/>
<xsl:sequence select="my:greet($who, 'Hello')"/>
</xsl:function>
<xsl:function name="my:greet">
<xsl:param name="who"/>
<xsl:param name="word"/>
<xsl:sequence select="concat($word, ', ', $who, '!')"/>
</xsl:function>