XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (308 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
2.83Mb size Format: txt, pdf, ePub

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:

  • The core library of XPath functions described in Chapter 13.
  • Additional functions defined by the host language in which XPath is embedded, for example, the XSLT functions which are also described in Chapter 13.
  • Constructor functions corresponding to the built-in atomic types in XML Schema, for example,
    xs:date()
    and
    xs:float()
    . For details of these functions, see Chapter 11.
  • Constructor functions corresponding to user-defined atomic types in any imported schema. These are also described in Chapter 11.
  • Additional functions made available by the vendor of the XPath processor. These should be in a namespace controlled by that vendor. An example is the function
    saxon:evaluate()
    offered by the Saxon product. For details, see the documentation supplied by the vendor.
  • User-written functions, written in XSLT or XQuery.
  • User-written functions (known as extension functions in XSLT, external functions in XQuery) written in an unrelated programming language, such as Java, C#, or JavaScript. The mechanisms for linking to such functions, and for converting values between the XPath data types and those of the target language, have been left to implementors to define.

Other books

Villette by Charlotte Bronte
Thirty Four Minutes DEAD by Kaye, Steve Hammond
The Fire Inside by Virginia Cavanaugh
Draw the Dark by Ilsa J. Bick
Dangerous When Wet: A Memoir by Jamie Brickhouse
Catch Me When I Fall by Westerhof Patricia
Ruthless Game by Christine Feehan
Point Me to Tomorrow by Veronica Chambers