XPath tutoriallogin
XPath tutorial
author:php.cn  update time:2022-04-20 14:10:21

XPath, XQuery, and XSLT functions


XPath, XQuery and XSLT FunctionsFunction Reference Manual


The following reference manual defines functions in XPath 2.0, .


Function Reference Manual

  • ##Access

  • Errors and Traces

  • Number

  • String

  • AnyURI

  • Logic

  • Duration/Date/Time

  • QName

    ##Node
  • Sequence
  • Context
The default prefix of the function namespace is fn:

lamp.gif

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

lamp.gif

Access function

##NameDescriptionfn:node-name(node)Returns the node name of the parameter node. fn:nilled(node)Returns a Boolean value of whether to reject the parameter node. fn:data(item.item,...)Accepts a sequence of items and returns a sequence of atomic values. ##fn:base-uri()Returns the value of the document-uri attribute of the specified node. Error and tracing functions
  • fn:base-uri(node)

  • Returns the value of the base-uri attribute of the current node or the specified node.
fn:document-uri(node)

NameDescriptionfn:error()Result: External The processing environment returns http://example.com/test#toohigh and the string "Error: Price is too high". is used to debug queries.
  • 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')

fn:trace(value,label)

Numeric functions

##fn:abs(num)fn:ceiling(num)##fn:floor(num)Return the largest integer not greater than the num parameter. ##fn:round(num)Example: round-half-to-even(0.5)Result: 0
NameDescription
fn: number(arg)

Returns the value of the parameter. Parameters can be booleans, strings, or node sets.

Example: number('100')

Result: 100

Return the absolute value of the parameter.

Example: abs(3.14)

Result: 3.14

Example: abs(-3.14)

Result: 3.14

Returns the smallest integer greater than the num parameter.

Example: ceiling(3.14)

Result: 4

Example: floor(3.14)

Result: 3

Round the num parameter to the nearest integer. Example: round(3.14)

Result: 3

##fn:round-half-to-even()

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

Result: 2

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

Result: 2

Functions related to strings

##fn:codepoints-to-string(int,int, ...)fn:string-to-codepoints(string)fn:codepoint-equal(comp1,comp2)According to the Unicode code point comparison, if the value of comp1 is equal to the value of comp2, true is returned. (http://www.w3.org/2005/02/xpath-functions/collation/codepoint), otherwise false is returned. ##fn:concat(string,string, ...)Returns the concatenation of strings. fn:string-join((string,string,...),sep)Use the sep parameter as the separator to return the string after the string parameters are spliced. ##fn:normalize-unicode( )Perform Unicode normalization. fn:upper-case(string)fn:lower-case( string)##fn:translate(string1, string2,string3)Replace string2 in string1 with string3. ##fn:escape-uri(stringURI,esc-res)fn:contains(string1,string2)fn:starts-with(string1, string2)fn:ends-with( string1,string2)fn:substring-before( string1,string2)fn: substring-after(string1,string2)fn: matches(string,pattern)
NameDescription
fn :string(arg)

Returns the string value of the parameter. Parameters can be numbers, logical values, or sets of nodes.

Example: string(314)

Result: "314"

Returns a string based on a sequence of code points.

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

Result: 'Thérèse'

Returns the code point sequence according to the string.

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

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

  • fn:compare(comp1,comp2)

  • fn:compare(comp1,comp2,collation )

#If comp1 is less than comp2, return -1. If comp1 is equal to comp2, 0 is returned. If comp1 is greater than comp2, return 1. (Depending on the comparison rules used).

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

Result: 0

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

Result: 'XPath is FUN!'

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)
Returns the specified starting position from the start position length of the substring. The index of the first character is 1. If the len argument is omitted, the substring from position start to the end of the string is returned.
Example: substring('Beatles',1,4)

Result: 'Beat'

Example: substring('Beatles',2)

Result: 'eatles'

fn:string-length(string)
  • fn:string-length()
#Returns the length of the specified string. If there is no string argument, returns the length of the current node's string value.
Example: string-length('Beatles')

Result: 7

  • fn:normalize-space(string)

  • fn:normalize-space()

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

Example: normalize-space(' The XML ')

Result: 'The XML'

Convert the string parameter to uppercase.

Example: upper-case('The XML')

Result: 'THE XML'

Convert the string parameter to lowercase.

Example: lower-case('The XML')

Result: 'the xml'

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'

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"

If string1 contains string2, return true, otherwise return false. Example: contains('XML','XM')

Result: true

If string1 starts with string2, returns true, otherwise returns false. Example: starts-with('XML','X')

Result: true

If string1 ends with string2, return true, otherwise return false. Example: ends-with('XML','X')

Result: false

Returns the substring of string2 before it appears in string1. Example: substring-before('12/10','/')

Result: '12'

Returns the substring after string2 appears in string1. Example: substring-after('12/10','/')

Result: '10'

Returns true if the string parameter matches the specified pattern, otherwise returns false. Example: matches("Merano", "ran")

Result: true

fn:replace(string,pattern,replace)

Replace the specified pattern with the replace parameter, and Return results.

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

Function for anyURI

##NameDescriptionfn:resolve-uri(relative,base)

Functions about Boolean values

NameDescriptionfn:boolean(arg)Returns a number, string or Boolean value for the node set. fn:not(arg)##fn:true()Return Boolean value true. ##fn:false()

Functions related to duration, date and time

Component extraction functions for date, time and duration

First restore the parameter to a Boolean value through the boolean() function. Returns true if the boolean value is false, true otherwise.

Example: not(true())

Result: false

Example: true()

Result: true

Return boolean false. Example: false()

Result: false

NameDescription
fn:dateTime(date,time)Convert the parameter to date and time.
fn:years-from-duration(datetimedur)Returns the integer of the year part of the parameter value, expressed in standard vocabulary notation.
fn:months-from-duration(datetimedur)Returns the integer of the month part of the parameter value, expressed in standard lexical notation.
fn:days-from-duration(datetimedur)Returns the integer of the days part of the parameter value, expressed in standard vocabulary notation.
fn:hours-from-duration(datetimedur)Returns the integer of the hour part of the parameter value, expressed in standard vocabulary notation.
fn:minutes-from-duration(datetimedur)Returns the integer of the minutes part of the parameter value, expressed in standard vocabulary notation.
fn:seconds-from-duration(datetimedur)Returns the decimal number of the minutes part of the parameter value, expressed in standard vocabulary notation.
fn:year-from-dateTime(datetime)

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

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

Result: 2005

fn:month-from-dateTime(datetime)

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

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

Result: 01

fn:day-from-dateTime(datetime)

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

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

Result: 10

fn:hours-from-dateTime(datetime)

Returns the integer 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 of the minutes part of the parameter'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 number of the seconds part of the parameter'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 time zone part of the parameter, if present.
fn:year-from-date(date)

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)

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)

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)Returns the time zone part of the parameter, if present.
fn:hours-from-time(time)

Returns the integer representing the hour part in the local value of the parameter.

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

Result: 10

fn:minutes-from-time(time)

Returns the integer representing the minute part in the local value of the parameter.

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 local value of the parameter.

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

Result: 0

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

Functions related to QNames

##fn:namespace-uri-for- prefix()##fn:in-scope-prefixes()fn: resolve-QName()

Functions about nodes

NameDescription
fn:QName()
fn:local-name-from-QName()
fn:namespace-uri-from-QName()
Returns the root node of the node tree to which the current node or the specified node belongs. Usually a document node.

Functions related to sequences

General functions

NameDescription
  • fn:name()

  • fn:name(nodeset)

Returns the name of the current node Or specify the first node in the 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)

##fn:remove((item,item,. ..),position) ##fn:empty(item,item,...)If the parameter value is an empty sequence, return true, otherwise return false. fn :exists(item,item,...)If the parameter value is not an empty sequence, return true, otherwise return false. ##fn:distinct-values ((item,item,...),collation)fn:insert-before((item,item,...),pos,inserts)fn:reverse((item,item,...))
NameDescription
fn:index-of((item,item,...),searchitem)

Returns the item sequence that is equal to the searchitem parameter Location.

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: ()

Returns a new sequence constructed by the item parameter - and deletes the item specified by the position parameter.

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

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

Result: false

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

Result: false

Returns the only different value. Example: distinct-values((1, 2, 3, 1, 2))

Result: (1, 2, 3)

Returns a new sequence constructed by the item parameter - while inserting inserts at the position specified by the pos parameter The value of the 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")

Returns the reverse order of the specified item. Example: reverse(("ab", "cd", "ef"))

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

Example: reverse(("ab"))

Result: ("ab")

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

Returns the position specified by the start parameter to return the item sequence, sequence The length of is specified by the len parameter. The first item's position is 1.

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

Result: ($item3,...)

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

Result: ($item2, $item3)

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

Function to test sequence capacity

NameDescription
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,...)If the parameter contains one or more items, return the parameter, otherwise generate 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 Descriptionfn:deep-equal(param1,param2,collation)If param1 and param2 are equal to each other (deep-equal), return true, otherwise return false.

Total function

##Namefn:count((item,item,...))fn:avg((arg,arg,...))##fn:max((arg,arg,...))Returns parameters that are greater than other parameters. Example: max((1,2,3)) Result: 3 Example: max(('a', 'k')) Result: 'k'fn:min((arg,arg,...))Return parameters that are smaller than other parameters. Example: min((1,2,3)) Result: 1 Example: min(('a', 'k')) Result: 'a'fn:sum(arg,arg,...)Returns the sum of the values ​​of each node in the specified node set.

Function to generate sequence

Description
Returns the number of nodes.
Returns the average of parameter values. Example: avg((1,2,3)) Result: 2
##fn:doc(URI)fn:doc- available(URI)If the doc() function returns the document node, it returns true, otherwise it returns false.
NameDescription
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:collection()
  • fn:collection(string)
Context function

Namefn:position()Returns the index position of the node currently being processed. fn :last()Returns the number of items in the list of nodes being processed. fn:current-dateTime( )fn:current-date()fn:current-time()fn:implicit-timezone()fn:default-collation()fn:static-base-uri()
Description

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

Result: Select the first three book elements

Example: //book[last()]

Result: Select the last book element

Returns the current dateTime (with time zone).
Returns the current date (with time zone).
Returns the current time (with time zone).
Returns the value of the implicit time zone.
Returns the value of the default collation.
Return the value of base-uri.