Functions

There are a variety of in-built functions for manipulating results.

count(node-set)

Returns the number of nodes in the context.

count(//td)

id()

Selects nodes by their ID attribute. Note that the ID attribute is declared in the DTD, not the literal id attribute. If it is not declared, the function cannot be used.


last(node-set)

Selects the last node in the set of nodes returned.

/table/tr[last()]

position()

Returns a number that is the position of the current context.

/table/tr[position()>3]

local-name(node-set)

Returns the localname of the node in the current context which is also the unprefixed name. If there are more than 1 node, it returns the local name of the first node.

local-name(/root/foo:animals)

name(node-set)

Returns the qualified name of the node in the current context. The qualified name includes the prefix, if it is present. Similarly, if there are more than 1 node, it returns the qualified name of the first node.

name(/root/foo:animals)

namespace-uri()

Returns the URI associated with the current context’s namespace.


concat(string, string, string*)

Joins the string arguments together, forming one string.

concat("a", "b", "c")

contains(string, string)

Returns true if the first argument string contains the second argument string; otherwise false.

contains("there", "here")

normalize-space(string)

Returns a string with leading and trailing white spaces removed.

normalize-space(" string with trailing spaces ")

starts-with(string, string)

Checks if the first argument begins with the second argument and returns a Boolean.

starts-with("christine", "chris")

string(obj)

Converts the argument into a string. When given a list of nodes, it converts the first node in the list.

string(//td/text())

string-length(string)

Returns the number of characters in the string.

string-length("123456")

substring-before(string, string)

Finds the first occurrence of the second argument from the first argument and returns the substring leading to it.

substring-before("http://www.foo.com", ".")

translate(string, string, string)

The translate function accepts 3 arguments. The first argument is the reference string. The second argument is a string of characters which are the characters to find from the reference string. The third argument is also a string of characters which are to replace the characters in the corresponding position in the second argument.

translate("http://www.foo.com", ".", "-")

boolean(expression)

The Boolean function converts the argument to a Boolean value. If the argument is a negative or positive number, it returns true. If the argument is zero or an NaN value, it returns false.

boolean(1)

false()

The false function always returns false. It accepts no arguments. It could be useful when used together with other conditional operators.

false()

true()

Similarly, the true function always returns true. It accepts no arguments. It could be useful when used together with other conditional operators.

true()

lang(string)

Returns true if the xml:lang attribute of the context node is the same as the argument string.

lang("en")

not(boolean)

Returns true if the argument is false, and vice versa.

not(boolean(1))

ceiling(number)

Returns the smallest integer greater or equals to the argument.

ceiling(1.6)

floor(number)

Returns the largest integer less than or equal to the argument.

floor(1.6)

number(obj)

Converts the argument to a number.

number("  - 9.7  ")

round(number)

Returns an integer nearest in value to the argument.

round(1.5)

sum(expression)

Returns the sum of all nodes in the node-set. Each node is first converted to a number value before aggregating into a sum.

sum(//td)