Name

[2.0] <xsl:function>

Defines a function that can be used in XPath expressions in the stylesheet.

Category

Declaration.

Required Attribute

name

The name of the stylesheet function.

Optional Attributes

as

Defines the datatype returned by this function.

override

Defines whether this function should override another function with the same name and the same number of arguments that is provided by the processor outside the scope of the stylesheet. override="yes", the default value, means this function will be used (it overrides the other function); override="no" means the other function will be used.

Content

Any number of <xsl:param> elements, followed by a sequence constructor. The created sequence is returned by the function.

Defined in

XSLT section 10.3, “Stylesheet Functions.”

Example

This stylesheet uses three <xsl:function>s to return the background color, font size, and style of various HTML elements. Putting the logic into a function means we can access the function from an attribute value template. Here’s the stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<!-- function.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sample="http://www.oreilly.com/catalog/xslt" exclude-result-prefixes="xs sample"> <xsl:output method="html" include-content-type="no"/> <xsl:variable name="colors" as="xs:string *" select="('yellow', 'white', 'blue')"/> <xsl:variable name="fontSizes" as="xs:integer *" select="(18, 24, ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.