Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The syntax of a function call consists of the function name, which is in general a
QName
, followed by the list of zero or more supplied arguments, in the usual way.
Each argument must be an
ExprSingle
. This basically means any XPath expression, as long as it does not contain a top-level
,
operator. If you want to supply a list of values separated by commas as a single argument, you must enclose it in parentheses. Note the difference between:
concat(“A”, “ nice”, “ cup”, “ of”, “ tea”)
which calls the
concat()
function with five separate arguments, each one a single string, and:
string-join((“A”, “nice”, “cup”, “of”, “tea”), “ ”)
which calls the
string-join()
function with two arguments, the first one being a sequence of five strings, and the second a string containing a single space character. These two function calls are both legal, and as it happens they both have the same effect. Both the functions are described in Chapter 13. The
concat()
function is exceptional in that it allows an arbitrary number of arguments to be supplied.
The arguments themselves can be expressions such as
/
,
.
, or
@*
, which may look a bit strange at first encounter. For example, the function call
exists(/*)
returns true if the context node is in a tree whose root is a document node that has an element node as a child.
Identifying the Function to Be Called
The set of functions that is available for calling is defined in the static context for the XPath expression, as described later in this chapter. This means that it is known at compile time whether a particular function name is valid or not. You can therefore expect a compile time error if you call a function that does not exist.
The function name is a QName. Like other QNames, it is written as a lexical QName (with an optional prefix and a local name, separated by a colon), and this lexical QName is expanded using the namespace declarations that are in scope for the XPath expression. So the expanded name of the function consists of a namespace URI and a local-name. The XPath static context includes a default namespace URI for function names, which will usually be quite separate from the default namespace URI for other kinds of name. Throughout this book I have assumed that the default namespace URI for functions will be
http://www.w3.org/2005/xpath-functions
, which contains the standard library of functions listed in Chapter 13. When this is the case, functions such as
count()
and
exists()
may be called with unprefixed names. XSLT requires that this is always the default namespace for function names, though other XPath environments may define a different default. You will often see function calls like
count()
written with a prefix as
fn:count()
, but this is never necessary in XSLT.
The function name is recognized in the XPath syntax by virtue of the fact that it is followed by a left parenthesis. This means that certain unprefixed names used as keywords in the language are not available for use as function names, specifically
attribute()
,
comment()
,
document-node()
,
element()
,
empty-sequence()
,
if()
,
item()
,
node()
,
processing-instruction()
,
schema-attribute()
,
schema- element()
,
text()
,
type()
, and
typeswitch()
. The name
typeswitch()
is not actually used as a keyword in XPath but is reserved for compatibility with XQuery.
The set of functions that are available for calling will generally include the following: