当前位置: 首页 > 网络学院 > XML相关教程 > XPath > XPath 函数

XPath
XPath 介绍
XPath 节点
XPath 语法
XPath 坐标轴
XPath 操作符
XPath 实例
XPath 摘要
XPath 函数

XPath 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 732 ::
收藏到网摘: n/a

The following reference library defines the functions required for XPath 2.0, XQuery 1.0 and XSLT 2.0.
下述参考目录库包含了 XPath 2.0、XQuery 1.0 和 XSLT 2.0 的必要函数。


Functions Reference
函数参考

The default prefix for the function namespace is fn:, and the URI is:
http://www.w3.org/2005/02/xpath-functions.
函数命名空间的默认前缀是“fn”,URI是:http://www.w3.org/2005/02/xpath-functions

Accessor Functions
访问器函数

Name
名称
Description
描述
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,...) Takes a sequence of items and returns a sequence of atomic values
获取项目的一个序列并返回原子值(atomic values)的一个序列
fn:base-uri()
fn:base-uri(node)
Returns the value of the base-uri property of the current or specified node
返回当前节点或指定节点内基于uri属性的值
fn:document-uri(node) Returns the value of the document-uri property for the specified node
返回指定节点中文档uri的属性值

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
向外部处理环境返回“ http://example.com/test#toohigh ” 和 “ Error: Price is too high ” 字符串

fn:trace(value,label) Used to debug queries
用来调试查询

Functions on Numeric Values
数值函数

ame
名称
Description
描述
fn:number(arg) Returns the numeric value of the argument. The argument could be a boolean, string, or node-set

返回自变量数值。这个自变量可以是一个逻辑值、字符串或者是节点集

Example: number('100')
Result: 100
案例: number('100')
结果: 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 that is greater than the number argument
返回大于指定数值自变量的最小整数

Example 案例: ceiling(3.14)
Result 结果 : 4

fn:floor(num) Returns the largest integer that is not greater than the number argument
返回不大于指定数值自变量的最大整数

Example 案例: floor(3.14)
Result 结果: 3

fn:round(num) Rounds the number 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 on Strings
字符串函数

Name
名称
Description
描述
fn:string(arg) Returns the string value of the argument. The argument could be a number, boolean, or 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 a sequence of code points from a string
返回一个字符串中的一组代码点序列

Example 案例: string-to-codepoints("Thérèse")
Result 结果: 84, 104, 233, 114, 232, 115, 101

fn:codepoint-equal(comp1,comp2) Returns true if the value of comp1 is equal to the value of comp2, according to the Unicode code point collation (http://www.w3.org/2005/02/xpath-functions/collation/codepoint), otherwise it returns false
根据统一字符编码点进行校正,如果comp1值与comp2的值等同,则返回true。(http://www.w3.org/2005/02/xpath-functions/collation/codepoint),反之则返回 false
fn:compare(comp1,comp2)
fn:compare(comp1,comp2,collation)
Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2 (according to the rules of the collation that is used)
如果comp1小于comp2则返回-1,如果comp1等于comp2,或者comp1大于comp2(根据校正规则)则返回0。

Example 案例: compare('ghi', 'ghi')
Result 结果: 0

fn:concat(string,string,...) Returns the concatenation of the strings
返回字符串的串联

Example 案例: concat('XPath ','is ','FUN!')
Result 结果: 'XPath is FUN!'

fn:string-join((string,string,...),sep) Returns a string created by concatenating the string arguments and using the sep argument as the separator
返回一个字符串,该字符串是由连接字符串自变量和使用类似于分离器功能的sep自变量创建的。

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 substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end
返回一个从起始位置算起到指定长度的子字符串。第一字符索引是1;如果长度未定义,则返回整段子字符串。

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 it returns the length of the string value of the current node
返回所指定字符串的长度。如果字符串自变量未定义,则返回当前节点的字符串值的长度。

Example 案例: string-length('Beatles')
Result 结果: 7

fn:normalize-space(string)
fn:normalize-space()
Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node
从指定字符串中删除最前端和最后端的空间,用一个空格替代所有的内部空间。如果字符串自变量未定义,则在当前节点中进行上述操作。

Example 案例: normalize-space(' The XML ')
Result 结果: 'The XML'

fn:normalize-unicode()  
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) Converts string1 by replacing the characters in string2 with the characters in string3
通过使用字符串3替换字符串2实现字符串1的转换

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 it returns false
如果字符串2与字符串1的全部或部分相匹配,则返回true;否则返回 false

Example 案例: contains('XML','XM')
Result 结果: true

fn:starts-with(string1,string2) Returns true if string1 starts with string2, otherwise it returns false
如果string 1以 string 2 为起始,则返回true;否则返回 false

Example 案例: starts-with('XML','X')
Result 结果: true

fn:ends-with(string1,string2) Returns true if string1 ends with string2, otherwise it returns false
如果string 1以string 2为终止,则返回true;否则返回 false

Example 案例: ends-with('XML','X')
Result 结果: false

fn:substring-before(string1,string2) Returns the start of string1 before string2 occurs in it
在string2触发之前返回 string1 的始端

Example 案例: substring-before('12/10','/')
Result 结果: '12'

fn:substring-after(string1,string2) Returns the remainder of string1 after string2 occurs in it
在string2 触发之前返回 string1 的余数

Example 案例: substring-after('12/10','/')
Result 结果: '10'

fn:matches(string,pattern) Returns true if the string argument matches the pattern, otherwise, it returns false
如果字符串自变量与模式相匹配,则返回true;否则返回 false

Example 案例: matches("Merano", "ran")
Result 结果: true

fn:replace(string,pattern,replace) Returns a string that is created by replacing the given pattern with the replace argument
使用给定的样式字符替换指定字符串自变量中的字符,并输出替换后的字符串

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
anyURI函数

Name
名称
Description
描述
fn:resolve-uri(relative,base)  

Functions on Boolean Values
逻辑值函数

Name
名称
Description
描述
fn:boolean(arg) Returns a boolean value for a number, string, or node-set
返回一个数字、字符串及节点集的逻辑值
fn:not(arg) The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true
通过使用请求 boolean() 函数将自变量转变成一个逻辑值。如果逻辑值是 false 则输出 true;如果逻辑值是true则输出false 。

Example 案例: not(true())
Result 结果: false

fn:true() Returns the boolean value true
返回的逻辑值为true

Example 案例: true()
Result 结果: true

fn:false() Returns the boolean value false
返回的逻辑值为false

Example 案例: false()
Result 结果: false

Functions on Durations, Dates and Times
时间段、日期和时间函数

Component Extraction Functions on Durations, Dates and Times
提取时间段函数、日期函数、时间函数的组件

Name
名称
Description
描述
fn:dateTime(date,time) Converts the arguments to a date and a time
把自变量转换成日期和时间
fn:years-from-duration(datetimedur) Returns an integer that represents the years component in the canonical lexical representation of the value of the argument
返回一个在自变量值的”标准词汇表示“中表示年份的整数
fn:months-from-duration(datetimedur) Returns an integer that represents the months component in the canonical lexical representation of the value of the argument
返回一个在自变量值的”标准词汇表示“中表示月份的整数
fn:days-from-duration(datetimedur) Returns an integer that represents the days component in the canonical lexical representation of the value of the argument
返回一个在自变量值的”标准词汇表示“中表示日的整数
fn:hours-from-duration(datetimedur) Returns an integer that represents the hours component in the canonical lexical representation of the value of the argument
返回一个在自变量值的”标准词汇表示“中表示小时的整数
fn:minutes-from-duration(datetimedur) Returns an integer that represents the minutes component in the canonical lexical representation of the value of the argument
返回一个在自变量值的”标准词汇表示“中表示分的整数
fn:seconds-from-duration(datetimedur) Returns a decimal that represents the seconds component in the canonical lexical representation of the value of the argument
返回一个在自变量值的”标准词汇表示“中表示秒的小数
fn:year-from-dateTime(datetime) Returns an integer that represents the year component in the localized value of the argument
返回一个在自变量的本地值中表示年的整数

Example 案例: year-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result 结果: 2005

fn:month-from-dateTime(datetime) Returns an integer that represents the month component in the localized value of the argument
返回一个在自变量的本地值中表示月的整数

Example 案例: month-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result 结果: 01

fn:day-from-dateTime(datetime) Returns an integer that represents the day component in the localized value of the argument
返回一个在自变量的本地值中表示日的整数

Example 案例: day-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result 结果: 10

fn:hours-from-dateTime(datetime) Returns an integer that represents the hours component in the localized value of the argument
返回一个在自变量的本地值中表示小时的整数

Example 案例: hours-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result 结果: 12

fn:minutes-from-dateTime(datetime) Returns an integer that represents the minutes component in the localized value of the argument
返回一个在自变量的本地值中表示分的整数

Example 案例: minutes-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result 结果: 30

fn:seconds-from-dateTime(datetime) Returns a decimal that represents the seconds component in the localized value of the argument
返回一个在自变量的本地值中表示秒的小数

Example 案例: seconds-from-dateTime(xs:dateTime("2005-01-10T12:30:00-04:10"))
Result 结果: 0

fn:timezone-from-dateTime(datetime) Returns the time zone component of the argument if any
返回所有自变量的时区组件
fn:year-from-date(date) Returns an integer that represents the year in the localized value of the argument
返回一个在自变量的本地值中表示年的整数

Example 案例: year-from-date(xs:date("2005-04-23"))
Result 结果: 2005

fn:month-from-date(date) Returns an integer that represents the month in the localized value of the argument
返回一个在自变量的本地值中表示月的整数

Example 案例: month-from-date(xs:date("2005-04-23"))
Result 结果: 4

fn:day-from-date(date) Returns an integer that represents the day in the localized value of the argument
返回一个在自变量的本地值中表示日的整数

Example 案例: day-from-date(xs:date("2005-04-23"))
Result 结果: 23

fn:timezone-from-date(date) Returns the time zone component of the argument if any
返回自变量的时区组件
fn:hours-from-time(time) Returns an integer that represents the hours component in the localized value of the argument
返回一个在自变量的本地值中表示小时的整数

Example 案例: hours-from-time(xs:time("10:22:00"))
Result 结果: 10

fn:minutes-from-time(time) Returns an integer that represents the minutes component in the localized value of the argument
返回一个在自变量的本地值中表示分的整数

Example 案例: minutes-from-time(xs:time("10:22:00"))
Result 结果: 22
fn:seconds-from-time(time) Returns an integer that represents the seconds component in the localized value of the argument
返回一个在自变量的本地值中表示秒的整数

Example 案例: seconds-from-time(xs:time("10:22:00"))
Result 结果: 0

fn:timezone-from-time(time) Returns the time zone component of the argument if any
返回自变量时区组件
fn:adjust-dateTime-to-timezone(datetime,timezone) If the timezone argument 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 argument 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 argument is empty, it returns a time without a timezone. Otherwise, it returns a time with a timezone
如果时区自变量为空,那么就返回一个不包含时区的时间值;反之,则返回一个包含时区的时间值

Functions Related to QNames
与QName相关的函数

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 on Nodes
节点函数

Name
名称
Description
描述
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
返回当前节点的命名空间URI或指定节点集中第一个节点的命名空间URI
fn:lang(lang) Returns true if the language of the current node matches the language of the specified language
如果当前节点的语言与指定语言的语言相匹配则返回true

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 of the tree to which the current node or the specified belongs. This will usually be a document node
返回当前节点或指定的从属节点所处的根目录。通常是一个文档节点。

Functions on Sequences
序列函数

General Functions on Sequences
基本序列函数

Name
名称
Description
描述
fn:index-of((item,item,...),searchitem) Returns the positions within the sequence of items that are equal to the searchitem argument
找到在指定的item序列中与searchitem自变量相匹配的位置,并返回该位置

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 value of the item arguments - with the item specified by the position argument removed
返回一个由指定item自变量值所构建的全新序列——其中包括被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 value of the arguments IS an empty sequence, otherwise it returns false
如果自变量值是一个空序列,则返回true;反之,则返回false

Example 案例: empty(remove(("ab", "cd"), 1))
Result 结果: false

fn:exists(item,item,...) Returns true if the value of the arguments IS NOT an empty sequence, otherwise it returns false
如果自变量值不是一个空序列,则返回true;反之,则返回false

Example 案例: exists(remove(("ab"), 1))
Result 结果: false

fn:distinct-values((item,item,...),collation) Returns only distinct (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 from the value of the item arguments - with the value of the inserts argument inserted in the position specified by the pos argument
返回一个由item自变量值构建的全新序列——包含被插入到由pos自变量指定的具体位置中的inserts自变量值

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 items specified
按照倒序形式返回指定项

Example 案例: reverse(("ab", "cd", "ef"))
Result 结果: ("ef", "cd", "ab")

Example 案例: reverse(("ab"))
Result 结果: ("ab")

fn:subsequence((item,item,...),start,len) Returns a sequence of items from the position specified by the start argument and continuing for the number of items specified by the len argument. The first item is located at position 1
返回从start自变量指定的具体位置开始,第”len字变量“个项。第一项处于 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 dependent order
按顺序返回一个执行程序中的项

Functions That Test the Cardinality of Sequences
基数序列测试函数

Name
名称
Description
描述
fn:zero-or-one(item,item,...) Returns the argument if it contains zero or one items, otherwise it raises an error
如果不包含任何项或只包含1项,则返回该自变量;反之,则会产生错误
fn:one-or-more(item,item,...) Returns the argument if it contains one or more items, otherwise it raises an error
如果包含1项或更多项则,则返回该自变量;反之,则会产生错误
fn:exactly-one(item,item,...) Returns the argument if it contains exactly one item, otherwise it raises an error
如果只包含1项则返回该自变量;反之,则会产生错误

Equals, Union, Intersection and Except
等于、与、或、非

Name
名称
Description
描述
fn:deep-equal(param1,param2,collation) Returns true if param1 and param2 are deep-equal to each other, otherwise it returns false
如果param1和param2位于同一级,则返回true;反之,则返回false

Aggregate Functions
集合函数

Name
名称
Description
描述
fn:count((item,item,...)) Returns the count 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 that is greater than the others
返回最大的自变量

Example 案例: max((1,2,3))
Result 结果: 3

Example 案例: max(('a', 'k'))
Result 结果: 'k'

fn:min((arg,arg,...)) Returns the argument that is 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 value of each node in the specified node-set
返回指定节点集中的所有节点之和

Example 案例: sum(1,2,3)
Result 结果: 6

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
返回元素节点序列,该元素节点的ID值与字符串自变量指定的一个或多个值的ID相等
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
返回元素或属性节点的序列,该元素或属性节点的IDREF值与字符串自变量中指定的一个或多个值的IDREF值相等
fn:doc(URI)  
fn:doc-available(URI) Returns true if the doc() function returns a document node, otherwise it returns false
如果doc()函数返回了一个文档节点,则返回true;反之,则返回false
fn:collection()
fn:collection(string)
 

Context Functions
段落函数

Name
名称
Description
描述
fn:position() Returns the index position of the node that is currently being processed
返回当前正在处理的节点索引位置

Example 案例: //book[position()<=3]
Result 结果: Selects the first three book elements

fn:last() Returns the number of items in the processed node list
返回已处理节点列表中的数字项

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 the base-uri
返回base-uri(基本URI)的值

评论 (0) All

登陆 | 还没注册?