XSLT, XPath, and XQuery Functions

XSLT 2.0, XPath 2.0, and XQuery 1.0 share the same function library.

Function Reference Manual

The default prefix of the function namespace is:fn:

The URI of the function namespace is:http://www.w3.org/2005/xpath-functions

Tip:It is usually used fn: prefix to call the function, for example fn:string()However, due to fn: It is the default namespace prefix, so the prefix is not needed when calling the function.

Access functions

Name Explaination
fn:node-name(node) Returns the node name of the argument node.
fn:nilled(node) Returns a boolean value indicating whether the argument node is nilled.
fn:data(item.item,...) Accepts an item sequence and returns an atomic value sequence.
  • fn:base-uri()
  • fn:base-uri(node)
Returns the value of the base-uri attribute of the current node or the specified node.
fn:document-uri(node) Returns the value of the document-uri attribute of the specified node.

Top

Error and trace functions

Name Explaination
  • fn:error()
  • fn:error(error)
  • fn:error(error,description)
  • fn:error(error,description,error-object)

Example: error(fn:QName('http://example.com/test', 'err:toohigh'), 'Error: Price is too high')

Result: Returns http://example.com/test#toohigh and the string "Error: Price is too high" to the external processing environment.

fn:trace(value,label) Used for debugging queries.

Top

Functions related to numbers

Name Explaination
fn:number(arg)

Returns the numeric value of the argument. The argument can be a boolean value, a string, or a node set.

Example: number('100')

Result: 100

fn:abs(num)

Return the absolute value of the parameter.

Example: abs(3.14)

Result: 3.14

Example: abs(-3.14)

Result: 3.14

fn:ceiling(num)

Return the smallest integer that is greater than the num parameter.

Example: ceiling(3.14)

Result: 4

fn:floor(num)

Return the largest integer that is not greater than the num parameter.

Example: floor(3.14)

Result: 3

fn:round(num)

Round the num parameter to the nearest integer.

Example: round(3.14)

Result: 3

fn:round-half-to-even()

Example: round-half-to-even(0.5)

Result: 0

Example: round-half-to-even(1.5)

Result: 2

Example: round-half-to-even(2.5)

Result: 2

Top

String functions

Name Explaination
fn:string(arg)

Return the string value of the argument. The argument can be a number, a logical value, or a node set.

Example: string(314)

Result: "314"

fn:codepoints-to-string(int,int,...)

According to the code point sequence, return the string.

Example: codepoints-to-string(84, 104, 233, 114, 232, 115, 101)

Result: 'Thérèse'

fn:string-to-codepoints(string)

According to the string, return the code point sequence.

Example: string-to-codepoints("Thérèse")

Result: 84, 104, 233, 114, 232, 115, 101

fn:codepoint-equal(comp1,comp2) According to the Unicode code point collation, if the value of comp1 is equal to the value of comp2, return true. (http://www.w3.org/2005/02/xpath-functions/collation/codepoint), otherwise return false.
  • fn:compare(comp1,comp2)
  • fn:compare(comp1,comp2,collation)

If comp1 is less than comp2, return -1. If comp1 is equal to comp2, return 0. If comp1 is greater than comp2, return 1. (According to the used collation rules).

Example: compare('ghi', 'ghi')

Result: 0

fn:concat(string,string,...)

Return the concatenation of strings.

Example: concat('XPath ','is ','FUN!')

Result: 'XPath is FUN!'

fn:string-join((string,string,...),sep)

Use the sep parameter as a separator to return the concatenated string of the string parameter.

Example: string-join(('We', 'are', 'having', 'fun!'), ' ')

Result: ' We are having fun! '

Example: string-join(('We', 'are', 'having', 'fun!'))

Result: 'Wearehavingfun!'

Example: string-join((), 'sep')

Result: ''

  • fn:substring(string,start,len)
  • fn:substring(string,start)

Return a substring of the specified length starting from the start position. The index of the first character is 1. If the len parameter is omitted, return the substring from position start to the end of the string.

Example: substring('Beatles',1,4)

Result: 'Beat'

Example: substring('Beatles',2)

Result: 'eatles'

  • fn:string-length(string)
  • fn:string-length()

Return the length of the specified string. If no string parameter is provided, return the length of the string value of the current node.

Example: string-length('Beatles')

Result: 7

  • fn:normalize-space(string)
  • fn:normalize-space()

Remove the leading and trailing whitespaces of the specified string, replace all internal whitespace sequences with one, and then return the result. If no string parameter is provided, the current node is processed.

Example: normalize-space(' The   XML ')

Result: 'The XML'

fn:normalize-unicode() Execute Unicode normalization.
fn:upper-case(string)

Convert the string parameter to uppercase.

Example: upper-case('The XML')

Result: 'THE XML'

fn:lower-case(string)

Convert the string parameter to lowercase.

Example: lower-case('The XML')

Result: 'the xml'

fn:translate(string1,string2,string3)

Replace string2 in string1 with string3.

Example: translate('12:30','30','45')

Result: '12:45'

Example: translate('12:30','03','54')

Result: '12:45'

Example: translate('12:30','0123','abcd')

Result: 'bc:da'

fn:escape-uri(stringURI,esc-res)

Example: escape-uri("http://example.com/test#car", true())

Result: "http%3A%2F%2Fexample.com%2Ftest#car"

Example: escape-uri("http://example.com/test#car", false())

Result: "http://example.com/test#car"

Example: escape-uri ("http://example.com/~bébé", false())

Result: "http://example.com/~b%C3%A9b%C3%A9"

fn:contains(string1,string2)

If string1 contains string2, return true; otherwise, return false.

Example: contains('XML','XM')

Result: true

fn:starts-with(string1,string2)

If string1 starts with string2, return true; otherwise, return false.

Example: starts-with('XML','X')

Result: true

fn:ends-with(string1,string2)

If string1 ends with string2, return true; otherwise, return false.

Example: ends-with('XML','X')

Result: false

fn:substring-before(string1,string2)

Return the substring before the occurrence of string2 in string1.

Example: substring-before('12/10','/')

Result: '12'

fn:substring-after(string1,string2)

Returns the substring after string2 in string1.

Example: substring-after('12/10','/')

Result: '10'

fn:matches(string,pattern)

Returns true if the string parameter matches the specified pattern, otherwise returns false.

Example: matches("Merano", "ran")

Result: true

fn:replace(string,pattern,replace)

Replaces the specified pattern with the replace parameter and returns the result.

Example: replace("Bella Italia", "l", "*")

Result: 'Be**a Ita*ia'

Example: replace("Bella Italia", "l", "")

Result: 'Bea Itaia'

fn:tokenize(string,pattern)

Example: tokenize("XPath is fun", "\s+")

Result: ("XPath", "is", "fun")

Top

Functions for anyURI

Name Explaination
fn:resolve-uri(relative,base)  

Top

Functions related to boolean values

Name Explaination
fn:boolean(arg) Returns the boolean value of a number, string, or node set.
fn:not(arg)

First, restore the parameter to a boolean value using the boolean() function.

Returns true if the boolean value is false, otherwise returns true.

Example: not(true())

Result: false

fn:true()

Returns the boolean value true.

Example: true()

Result: true

fn:false()

Returns the boolean value false.

Example: false()

Result: false

Top

Functions related to duration, date, and time

Function to extract components of date, time, and duration

Name Explaination
fn:dateTime(date,time) Converts the parameter to a date and time.
fn:years-from-duration(datetimedur) Returns the integer part of the year component of the parameter value, represented in standard lexical notation.
fn:months-from-duration(datetimedur) Returns the integer part of the month component of the parameter value, represented in standard lexical notation.
fn:days-from-duration(datetimedur) Returns the integer part of the days part of the parameter value, represented in standard lexical notation.
fn:hours-from-duration(datetimedur) Returns the integer part of the hours part of the parameter value, represented in standard lexical notation.
fn:minutes-from-duration(datetimedur) Returns the integer part of the minutes part of the parameter value, represented in standard lexical notation.
fn:seconds-from-duration(datetimedur) Returns the decimal number of the minutes part of the parameter value, represented in standard lexical notation.
fn:year-from-dateTime(datetime)

Returns the integer part of the year part of the local value of the parameter.

Example: year-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))

Result: 2005

fn:month-from-dateTime(datetime)

Returns the integer part of the month part of the local value of the parameter.

Example: month-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))

Result: 01

fn:day-from-dateTime(datetime)

Returns the integer part of the day part of the local value of the parameter.

Example: day-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))

Result: 10

fn:hours-from-dateTime(datetime)

Returns the integer part of the hours part of the local value of the parameter.

Example: hours-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))

Result: 12

fn:minutes-from-dateTime(datetime)

Returns the integer part of the minutes part of the local value of the parameter.

Example: minutes-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))

Result: 30

fn:seconds-from-dateTime(datetime)

Returns the decimal number of the seconds part of the local value of the parameter.

Example: seconds-from-dateTime(xs:dateTime("2005-01-10T12:30:00-04:10"))

Result: 0

fn:timezone-from-dateTime(dateTime) It returns the timezone part of the parameter if it exists.
fn:year-from-date(date)

It returns the integer representing the year in the local value of the parameter.

Example: year-from-date(xs:date("2005-04-23"))

Result: 2005

fn:month-from-date(date)

It returns the integer representing the month in the local value of the parameter.

Example: month-from-date(xs:date("2005-04-23"))

Result: 4

fn:day-from-date(date)

It returns the integer representing the day in the local value of the parameter.

Example: day-from-date(xs:date("2005-04-23"))

Result: 23

fn:timezone-from-date(date) It returns the timezone part of the parameter if it exists.
fn:hours-from-time(time)

It returns the integer representing the hours part in the local value of the parameter.

Example: hours-from-time(xs:time("10:22:00"))

Result: 10

fn:minutes-from-time(time)

It returns the integer representing the minutes part in the local value of the parameter.

Example: minutes-from-time(xs:time("10:22:00"))

Result: 22

fn:seconds-from-time(time)

It returns the integer representing the seconds part in the local value of the parameter.

Example: seconds-from-time(xs:time("10:22:00"))

Result: 0

fn:timezone-from-time(time) It returns the timezone part of the parameter if it exists.
fn:adjust-dateTime-to-timezone(dateTime,timezone)

If the timezone parameter is empty, it returns a dateTime without a timezone.

Otherwise, it returns a dateTime with a timezone.

fn:adjust-date-to-timezone(date,timezone)

If the timezone parameter is empty, it returns a date without a timezone.

Otherwise, it returns a date with a timezone.

fn:adjust-time-to-timezone(time,timezone)

If the timezone parameter is empty, it returns time without a timezone.

Otherwise, return the time with the time zone.

Top

Functions related to QNames

Name Explaination
fn:QName()  
fn:local-name-from-QName()  
fn:namespace-uri-from-QName()  
fn:namespace-uri-for-prefix()  
fn:in-scope-prefixes()  
fn:resolve-QName()  

Top

Functions related to nodes

Name Explaination
  • fn:name()
  • fn:name(nodeset)
Returns the name of the current node or the first node in the specified node set.
  • fn:local-name()
  • fn:local-name(nodeset)
Returns the name of the current node or the first node in the specified node set without the namespace prefix.
  • fn:namespace-uri()
  • fn:namespace-uri(nodeset)
Returns the namespace URI of the current node or the first node in the specified node set.
fn:lang(lang)

Returns true if the language of the current node matches the specified language.

Example: Lang("en") is true for <p xml:lang="en">...</p>

Example: Lang("de") is false for <p xml:lang="en">...</p>

  • fn:root()
  • fn:root(node)
Returns the root node of the node tree to which the current node or specified node belongs. It is usually the document node.

Top

Functions related to sequences

General function

Name Explaination
fn:index-of((item,item,...),searchitem)

Returns the position of the item in the item sequence that is equal to the searchitem parameter.

Example: index-of ((15, 40, 25, 40, 10), 40)

Result: (2, 4)

Example: index-of (("a", "dog", "and", "a", "duck"), "a")

Result (1, 4)

Example: index-of ((15, 40, 25, 40, 10), 18)

Result: ()

fn:remove((item,item,...),position)

Returns a new sequence constructed by the item parameter - while deleting the item at the specified position.

Example: remove(("ab", "cd", "ef"), 0)

Result: ("ab", "cd", "ef")

Example: remove(("ab", "cd", "ef"), 1)

Result: ("cd", "ef")

Example: remove(("ab", "cd", "ef"), 4)

Result: ("ab", "cd", "ef")

fn:empty(item,item,...)

Returns true if the parameter value is an empty sequence, otherwise returns false.

Example: empty(remove(("ab", "cd"), 1))

Result: false

fn:exists(item,item,...)

Returns true if the parameter value is not an empty sequence, otherwise returns false.

Example: exists(remove(("ab"), 1))

Result: false

fn:distinct-values((item,item,...),collation)

Returns unique different values.

Example: distinct-values((1, 2, 3, 1, 2))

Result: (1, 2, 3)

fn:insert-before((item,item,...),pos,inserts)

Returns a new sequence constructed by the item parameter, inserting the value of the inserts parameter at the position specified by the pos parameter.

Example: insert-before(("ab", "cd"), 0, "gh")

Result: ("gh", "ab", "cd")

Example: insert-before(("ab", "cd"), 1, "gh")

Result: ("gh", "ab", "cd")

Example: insert-before(("ab", "cd"), 2, "gh")

Result: ("ab", "gh", "cd")

Example: insert-before(("ab", "cd"), 5, "gh")

Result: ("ab", "cd", "gh")

fn:reverse((item,item,...))

Returns the reversed order of the specified items.

Example: reverse(("ab", "cd", "ef"))

Result: ("ef", "cd", "ab")

Example: reverse(("ab"))

Result: ("ab")

fn:subsequence((item,item,...),start,len)

Returns the item sequence at the position specified by the start parameter, with the length specified by the len parameter.

The position of the first item is 1.

Example: subsequence(($item1, $item2, $item3,...), 3)

Result: ($item3, ...)

Example: subsequence(($item1, $item2, $item3, ...), 2, 2)

Result: ($item2, $item3)

fn:unordered((item,item,...)) Returns items in the order determined by the implementation.

Function to test the capacity of a sequence

Name Explaination
fn:zero-or-one(item,item,...) Returns the parameter if it contains zero or one item, otherwise generates an error.
fn:one-or-more(item,item,...) Returns the parameter if it contains one or more items, otherwise generates an error.
fn:exactly-one(item,item,...) Returns the parameter if it contains an item, otherwise generates an error.

Equals, Union, Intersection and Except

Name Explaination
fn:deep-equal(param1,param2,collation) Returns true if param1 and param2 are equal to each other (deep-equal), otherwise returns false.

Aggregate functions

Name Explaination
fn:count((item,item,...)) Returns the number of nodes.
fn:avg((arg,arg,...))

Returns the average of the parameter values.

Example: avg((1,2,3))

Result: 2

fn:max((arg,arg,...))

Returns the parameter that is greater than the other parameters.

Example: max((1,2,3))

Result: 3

Example: max(('a', 'k'))

Result: 'k'

fn:min((arg,arg,...))

Returns the parameter that is less than the other parameters.

Example: min((1,2,3))

Result: 1

Example: min(('a', 'k'))

Result: 'a'

fn:sum(arg,arg,...) Returns the sum of the numeric values of each node in the specified node set.

Function to generate sequences

Name Explaination
fn:id((string,string,...),node) Returns a sequence of element nodes whose ID attribute values are equal to one or more values specified in the string parameter.
fn:idref((string,string,...),node) Returns a sequence of element or attribute nodes whose IDREF attribute values are equal to one or more values specified in the string parameter.
fn:doc(URI)  
fn:doc-available(URI) Returns true if the doc() function returns a document node, otherwise returns false.
  • fn:collection()
  • fn:collection(string)
 

Top

Context Functions

Name Explaination
fn:position()

Return the index position of the current node being processed.

Example: //book[position()<=3]

Result: Select the first three book elements

fn:last()

Return the number of items in the list of nodes being processed.

Example: //book[last()]

Result: Select the last book element

fn:current-dateTime() Return the current dateTime (with timezone).
fn:current-date() Return the current date (with timezone).
fn:current-time() Return the current time (with timezone).
fn:implicit-timezone() Return the value of the implicit timezone.
fn:default-collation() Return the value of the default collation.
fn:static-base-uri() Return the value of base-uri.

Top

XSLT Functions

In addition, there are the following built-in XSLT functions:

Name Description
current() Return a node set containing the current node as its only member.
document() Used to access nodes in an external XML document.
element-available() Detect whether the XSLT processor supports the specified element.
format-number() Convert a number to a string.
function-available() Detect whether the XSLT processor supports the specified function.
generate-id() Return the string value that uniquely identifies the specified node.
key() Retrieve elements previously marked with the <xsl:key> statement.
node-set Convert a tree to a node set. The resulting node set always contains a single node and is the root node of the tree.
system-property() Return the value of the system property.
unparsed-entity-uri() Return the URI of the unparsed entity.

Top