Chapter 5. XPath and XSLT Functions

XPath defines 27 functions that may be called in expressions, including predicates. XSLT adds 9 functions to the XPath mix. You have already seen a number of these functions at work in previous examples in this book, such as name( ), string-length( ), and substring( ).

Chapter 5 briefly documents all of the XPath 1.0 functions, including arguments. Similarly, Chapter 5 describes the XSLT 1.0 functions. XPath functions are divided into 4 types: Boolean, node-set, number, and string.

Node tests such as text( ) and comment( ) look like functions but they are not really functions. Node tests simply test whether a given node matches a node type.


I'll demonstrate a number of functions in this chapter, but other chapters in this book will expose you to more examples of functions. For additional details on XPath functions, see Section 4 of the XPath specification. You can read more about XSLT functions in Sections 12 through 15 of the XSLT specification.

This is an introductory book and it does not provide exhaustive explanations of all of the XPath functions, though it will give you a chance to use many of them. John Simpson's XPath and XPointer (O'Reilly) explores XPath (and its derivative XPointer) in greater detail. Doug Tidwell's XSLT (O'Reilly), provides an excellent reference that demonstrates all these functions, as does Michael Kay's XSLT Programmer's Reference (Wrox). Another resource is Mulberry Technologies' XSLT and XPath Quick Reference at http://www.mulberrytech.com/quickref/XSLTquickref.pdf.


Table 5-1. XPath functions

XPath function signature

Type

Returns

Description

boolean(object)

Boolean

Boolean

Converts the argument to a Boolean.

ceiling(number)

Number

Number

Returns the smallest integer (closest to negative infinity) that is not less than the argument.

concat(string, string, string*)

String

String

Returns a concatenation of two or more string arguments.

contains(string, string)

String

Boolean

Returns true if the first argument contains the string in the second argument.

count(node-set)

Node-set

Number

Returns the number of nodes in a node-set.

false( )

Boolean

Boolean

Returns false.

floor(number)

Number

Number

Returns the largest integer (closest to positive infinity) that is not less than the argument.

id(object)

Node-set

Node-set

Selects elements by unique ID.

lang(string)

Boolean

Boolean

Returns true if the argument matches the context node's xml:lang value.

last( )

Node-set

Number

Returns the context size (the node position of the last node).

local-name(node-set?)

Node-set

String

Returns the local part of an expanded QName (without a prefix).

name(node-set?)

Node-set

String

Returns a QName.

namespace-uri(node-set?)

Node-set

String

Returns a namespace URI.

normalize-space(string?)

String

String

Returns a string with its whitespace normalized.

not(boolean)

Boolean

Boolean

Returns true if the argument is false.

number(object?)

Number

Number

Converts an object to a number.

position( )

Node-set

Number

Returns the context position (of the current node).

round(number)

Number

Number

Returns an integer, rounded closest to the value of the argument.

starts-with(string, string)

String

Boolean

Returns true if the first string starts with the second; otherwise it returns false.

string(object?)

String

String

Converts an object into a string.

string-length(string?)

String

Number

Returns the length of a string.

substring(string, number, number?)

String

String

Returns a substring of the first argument, based on the starting position in the second argument, and the optional length in the third.

substring-after(string, string)

String

String

Returns the substring of the first argument that follows the first occurrence of the second argument in the first.

substring-before(string, string)

String

String

Returns the substring of the first argument that precedes the first occurrence of the second argument in the first.

sum(node-set)

Number

Number

Returns a sum of nodes.

translate(string, string, string)

String

String

Returns the first argument string with occurrences of characters in the second replaced by corresponding characters in the third.

true( )

Boolean

Boolean

Returns true.

Table 5-2. XSLT functions

XSLT function signature

Returns

Description

document(object, node-set?)

Node-set

Loads additional documents, allowing a transformation to have more than one input document.

key(string, object)

Node-set

Applies the key in the first argument using the second argument.

format-number(number, string, string?)

String

Converts a number to a string using the format pattern in the second argument and the decimal format identified by the third argument, if present.

current( )

Node-set

Returns the current node.

unparsed-entity-uri(string)

String

Returns a URI for an unparsed entity.

generate-id(node-set?)

String

Generates a unique ID for an element in the result tree.

system-property(string)

Object

Returns an XSLT system property.

element-available(string)

Boolean

Returns true if the named element is available.

function-available(string)

Boolean

Returns true if the named function is available.

If you need even more functions, consider exploring the EXSLT extensions library, explained in Chapter 15.




Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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