Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XPath, XQuery and XSLT Functions

XPath, XQuery and XSLT Function Reference


The reference manual below defines the functions in XPath 2.0, XQuery 1.0 and XSLT 2.0.


Function reference

|

| | | | --- | --- | --- |

Note The default prefix of the function namespace is fn:

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

Accessor functions

Name Description
fn:node-name(node) Returns the node name of the argument node.
fn:nilled(node) Returns a boolean indicating whether the argument node is rejected.
fn:data(item.item,...) Accepts a sequence of items and returns a sequence of atomic values.
  • fn:base-uri()
  • fn:base-uri(node)
  • | Returns the value of the base-uri property of the current or specified node. | | fn:document-uri(node) | Returns the value of the document-uri property of the specified node. |

    Error and trace functions

    Name Description
    • 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 to debug queries. |

      Functions for numeric values

      Name Description
      fn:number(arg)

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

      Example: number('100')

      Result: 100

      fn:abs(num)

      Returns the absolute value of the argument.

      Example: abs(3.14)

      Result: 3.14

      Example: abs(-3.14)

      Result: 3.14

      fn:ceiling(num)

      Returns the smallest integer greater than the num argument.

      Example: ceiling(3.14)

      Result: 4

      fn:floor(num)

      Returns the largest integer not greater than the num argument.

      Example: floor(3.14)

      Result: 3

      fn:round(num)

      Rounds the num argument 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

      Functions for strings

      Name Description
      fn:string(arg)

      Returns the string value of the argument. The argument may be a number, a boolean or a node-set.

      Example: string(314)

      Result: "314"

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

      Returns a string from a sequence of code points.

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

      Result: 'Thérèse'

      fn:string-to-codepoints(string)

      Returns the sequence of code points for a string.

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

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

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

        Returns -1 if comp1 is less than comp2, 0 if comp1 equals comp2, and 1 if comp1 is greater than comp2 (according to the collation rules in use).

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

        Result: 0

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

        Returns the concatenation of strings.

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

        Result: 'XPath is FUN!'

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

        Uses the sep argument as the separator and returns the concatenation of the string arguments.

        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: ''

        | | |

        Returns a substring of the specified length starting at the start position. The first character has index 1. If the len argument is omitted, returns the substring from position start to the end of the string.

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

        Result: 'Beat'

        Example: substring('Beatles',2)

        Result: 'eatles'

        | | |

        Returns the length of the specified string. If there is no string argument, returns the length of the string value of the current node.

        Example: string-length('Beatles')

        Result: 7

        | | |

        Removes leading and trailing whitespace from the specified string, replaces every internal sequence of whitespace with a single one, and returns the result. If there is no string argument, the current node is processed.

        Example: normalize-space(' The XML ')

        Result: 'The XML'

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

        Converts the string argument to upper case.

        Example: upper-case('The XML')

        Result: 'THE XML'

        | | fn:lower-case(string) |

        Converts the string argument to lower case.

        Example: lower-case('The XML')

        Result: 'the xml'

        | | fn:translate(string1,string2,string3) |

        Replaces 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) |

        Returns true if string1 contains string2, otherwise false.

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

        Result: true

        | | fn:starts-with(string1,string2) |

        Returns true if string1 starts with string2, otherwise false.

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

        Result: true

        | | fn:ends-with(string1,string2) |

        Returns true if string1 ends with string2, otherwise false.

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

        Result: false

        | | fn:substring-before(string1,string2) |

        Returns the substring of string1 before the first occurrence of string2.

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

        Result: '12'

        | | fn:substring-after(string1,string2) |

        Returns the substring of string1 after the first occurrence of string2.

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

        Result: '10'

        | | fn:matches(string,pattern) |

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

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

        Result: true

        | | fn:replace(string,pattern,replace) |

        Replaces the specified pattern with the replace argument 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")

        |

        Functions for anyURI

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

        Functions for boolean values

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

        First converts the argument to a boolean with the boolean() function. Returns true if that boolean is false, and false if that boolean is 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

        Functions for durations, dates and times

        Component extraction functions for dates, times and durations

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

        Returns the integer year part of the argument's local value.

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

        Result: 2005

        fn:month-from-dateTime(datetime)

        Returns the integer month part of the argument's local value.

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

        Result: 01

        fn:day-from-dateTime(datetime)

        Returns the integer day part of the argument's local value.

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

        Result: 10

        fn:hours-from-dateTime(datetime)

        Returns the integer hours part of the argument's local value.

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

        Result: 12

        fn:minutes-from-dateTime(datetime)

        Returns the integer minutes part of the argument's local value.

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

        Result: 30

        fn:seconds-from-dateTime(datetime)

        Returns the decimal seconds part of the argument's local value.

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

        Result: 0

        fn:timezone-from-dateTime(datetime) Returns the timezone part of the argument, if present.
        fn:year-from-date(date)

        Returns the integer representing the year in the argument's local value.

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

        Result: 2005

        fn:month-from-date(date)

        Returns the integer representing the month in the argument's local value.

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

        Result: 4

        fn:day-from-date(date)

        Returns the integer representing the day in the argument's local value.

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

        Result: 23

        fn:timezone-from-date(date) Returns the timezone part of the argument, if present.
        fn:hours-from-time(time)

        Returns the integer representing the hours part in the argument's local value.

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

        Result: 10

        fn:minutes-from-time(time)

        Returns the integer representing the minutes part in the argument's local value.

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

        Result: 22

        fn:seconds-from-time(time)

        Returns the integer representing the seconds part in the argument's local value.

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

        Result: 0

        fn:timezone-from-time(time) Returns the timezone part of the argument, if present.
        fn:adjust-dateTime-to-timezone(datetime,timezone) If the timezone argument is empty, returns a dateTime without a timezone. Otherwise returns a dateTime with a timezone.
        fn:adjust-date-to-timezone(date,timezone) If the timezone argument is empty, returns a date without a timezone. Otherwise returns a date with a timezone.
        fn:adjust-time-to-timezone(time,timezone) If the timezone argument is empty, returns a time without a timezone. Otherwise returns a time with a timezone.

        Functions related to QNames

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

        Functions for nodes

        Name Description
        • fn:name()
        • fn:name(nodeset)
        • | Returns the name of the current node or the first node in the specified node-set. | | | Returns the name of the current node or the first node in the specified node-set - without the namespace prefix. | | | 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

          ...

          Example: Lang("de") is false for

          ...

          | | | Returns the root node of the node tree to which the current node or the specified node belongs. Usually the document node. |

          Functions for sequences

          General functions

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

          Returns the positions in the item sequence that equal the searchitem argument.

          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 from the item argument, with the item at the position given by the position argument removed.

          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 argument value is an empty sequence, otherwise false.

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

          Result: false

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

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

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

          Result: false

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

          Returns the unique distinct 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 from the item argument with the inserts values inserted at the position given by the pos argument.

          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 specified items in reverse order.

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

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

          Example: reverse(("ab"))

          Result: ("ab")

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

          Returns the item sequence starting at the position given by the start argument, with a length given by the len argument. The first item is at position 1.

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

          Result: ($item3, ...)

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

          Result: ($item2, $item3)

          fn:unordered((item,item,...)) Returns the items in an implementation-defined order.

          Functions that test sequence capacity

          Name Description
          fn:zero-or-one(item,item,...) Returns the argument if it contains zero or one item, otherwise raises an error.
          fn:one-or-more(item,item,...) Returns the argument if it contains one or more items, otherwise raises an error.
          fn:exactly-one(item,item,...) Returns the argument if it contains exactly one item, otherwise raises an error.

          Equals, Union, Intersection and Except

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

          Aggregate functions

          Name Description
          fn:count((item,item,...)) Returns the number of nodes.
          fn:avg((arg,arg,...)) Returns the average of the argument values. Example: avg((1,2,3)) Result: 2
          fn:max((arg,arg,...)) Returns the argument greater than the others. Example: max((1,2,3)) Result: 3 Example: max(('a', 'k')) Result: 'k'
          fn:min((arg,arg,...)) Returns the argument less than the others. 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.

          Functions that generate sequences

          Name Description
          fn:id((string,string,...),node) Returns a sequence of element nodes that have an ID value equal to the value of one or more of the values specified in the string argument
          fn:idref((string,string,...),node) Returns a sequence of element or attribute nodes that have an IDREF value equal to the value of one or more of the values specified in the string argument
          fn:doc(URI)  
          fn:doc-available(URI) Returns true if the doc() function returns a document node, otherwise false.
          • fn:collection()
          • fn:collection(string)
          • |   |

            Context functions

            Name Description
            fn:position()

            Returns the index position of the node currently being processed.

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

            Result: selects the first three book elements

            fn:last()

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

            Example: //book[last()]

            Result: selects the last book element

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