Introducing the XPath 2.0 Functions

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 

Function Namespaces

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.

SHOP TALK : IMPLEMENTATION DEFINITIONS VERSUS SPECIFICATION DEFINITIONS

The fn: prefix, used in the XPath 2.0 functions specification, is not used in Saxon. Technically, the XPath 2.0 functions should be in the "http://www.w3.org/2003/05/xpath-functions" namespace, but the current versions of Saxon don't do things that way in order to retain compatibility with XPath 1.0.

This brings up an interesting pointmuch in the XPath 2.0 specifications is still defined by the implementation, not by XPath 2.0. As you go through the XPath 2.0 specifications, you'll often find the terms "implementation-defined" and "implementation-dependent," and those are items to be aware of.

For example, the precision of numbers returned by the numeric XPath 2.0 functions is actually defined by the implementation, not by XPath 2.0 (except for those functions that let you actually set the precision yourself).

And the expanded names returned by functions like fn:node-name consist of a pair of values, a namespace URI, and a local name. The way this return value is actually handled is also being left up to the implementation.

And there are many other such issues as wellhow non-XML media types are handled, for example, whether the implementation uses DTD or schema validation to check a document's syntax, the order of nodes returned in some functions, and so on.

Some of these issuessuch as whether or not you need to preface function names with fn: make a big difference to you before you use a particular XPath processor. Some, such as which of two identical maximum values in a sequence are returned by the fn:max function, probably won't. It's going to be interesting to see how these issues shake out in the long run.


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.



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net