This chapter starts our coverage of the many functions built into XPath 2.0. Although we've seen structural changes in XPath 2.0 compared to XPath 1.0 in the preceding two chapters, on a sheer size basis, there's no question that all the new functions added to XPath 2.0 are the biggest change.
The specification for the XPath 2.0 functions appears in the document XQuery 1.0 and XPath 2.0 Functions and Operators, which is at http://www.w3.org/TR/xquery-operators.
This document is an integral part of the XPath 2.0 specification. Not only are these functions now built into XPath 2.0, but they're also built into XML Query 1.0 and XSLT 2.0. The functions we're going to discuss in this and the following chapters were specifically designed to work on the XPath 2.0 data types, which we covered in Chapter 7, "Introducing XPath 2.0?"
Formally, each function is given by its signature , which shows you how to use the function. A signature shows what parameters you pass to a function, and the type of return value, if there is one:
function-name ( $parameter-name as parameter-type , ...) as return-type
For example, the signature of the node-name function, which returns the name of the node you pass to it as a string, looks like this:
node-name ($srcval as node ) as xs:string
The function name itself for all the functions we'll be taking a look at is an XML QName, followed by parentheses. The functions and operators in the XQuery 1.0 and XPath 2.0 Functions and Operators document use various namespaces, which you should know about.
For data types, the xs namespace is used for standard XML-schema types, as before; this namespace corresponds to "http://www.w3.org/2001/XMLSchema". And the xdt namespace, which corresponds to "http://www.w3.org/2003/05/xpath-datatypes", is the namespace for the data types specifically added for XPath 2.0.
The namespace for functions is identified by the fn prefix. Its value is "http://www.w3.org/2003/05/xpath-functions". You can call functions with this namespace prefix; for example, the node-name function is referred to as fn:node-name in the XQuery 1.0 and XPath 2.0 Functions and Operators document:
fn:node-name ($srcval as node ) as xs:string
Saxon supports many of the XPath 2.0 functions we're going to see, but you do not use the fn prefix when calling these functions in Saxon.
There's also a fourth prefix defined in the XQuery 1.0 and XPath 2.0 Functions and Operators documentthe op prefix, which corresponds to the namespace "http://www.w3.org/2003/05/xpath-operators for operators", and which you use with operators. The operator functions are included in the XQuery 1.0 and XPath 2.0 Functions and Operators document to make clear how the various XPath 2.0 operators work on various data types, and they're there to back up the standard operators, which we saw in Chapter 8, "XPath 2.0 Expressions, Sequences, and Operators." For example, the * operator on numeric values has a corresponding function named op:numeric-multiply :
op:multiply( $operand1 as numeric, $operand2 as numeric ) as numeric
The op functions are not directly accessible in XPath 2.0, so you cannot call them directly (some software may make them accessible, but Saxon does not). They're in the specification to make it clear how the operators we covered in Chapter 8 work on various data typeswe've already covered those operators, so we won't give much coverage to the op functions here. However, if you need to know exactly how, say, xs:date values work when you subtract one from another, you can get all the details by looking at how op:subtract-dates is defined for xs:date values in the XQuery 1.0 and XPath 2.0 Functions and Operators document.
The XQuery 1.0 and XPath 2.0 Functions and Operators document starts off with the specification for the accessor functions , which work on nodes, and we'll start there too.