XPath、XQuery 以及 XSLT 函數函數參考手冊
下麵的參考手冊定義了XPath 2.0,XQuery 1.0和XSLT 2.0中的函數。
函數參考手冊
函數命名空間的默認首碼為 fn:
函數命名空間的 URI為 : http://www.w3.org/2005/xpath-functions
存取函數
名稱 | 說明 |
---|---|
fn:node-name(node) | 返回參數節點的節點名稱。 |
fn:nilled(node) | 返回是否拒絕參數節點的布爾值。 |
fn:data(item.item,...) | 接受專案序列,並返回原子值序列。 |
|
返回當前節點或指定節點的 base-uri 屬性的值。 |
fn:document-uri(node) | 返回指定節點的 document-uri 屬性的值。 |
錯誤和跟蹤函數
名稱 | 說明 |
---|---|
|
例子:error(fn:QName('http://example.com/test', 'err:toohigh'), 'Error: Price is too high') 結果:向外部處理環境返回 http://example.com/test#toohigh 以及字串 "Error: Price is too high"。 |
fn:trace(value,label) | 用於對查詢進行 debug。 |
有關數值的函數
名稱 | 說明 |
---|---|
fn:number(arg) |
返回參數的數值。參數可以是布爾值、字串或節點集。 例子:number('100') 結果:100 |
fn:abs(num) |
返回參數的絕對值。 例子:abs(3.14) 結果:3.14 例子:abs(-3.14) 結果:3.14 |
fn:ceiling(num) |
返回大於 num 參數的最小整數。 例子:ceiling(3.14) 結果:4 |
fn:floor(num) |
返回不大於 num 參數的最大整數。 例子:floor(3.14) 結果:3 |
fn:round(num) |
把 num 參數舍入為最接近的整數。 例子:round(3.14) 結果:3 |
fn:round-half-to-even() |
例子:round-half-to-even(0.5) 結果:0 例子:round-half-to-even(1.5) 結果:2 例子:round-half-to-even(2.5) 結果:2 |
有關字串的函數
名稱 | 說明 |
---|---|
fn:string(arg) |
返回參數的字串值。參數可以是數字、邏輯值或節點集。 例子:string(314) 結果:"314" |
fn:codepoints-to-string(int,int,...) |
根據代碼點序列返回字串。 例子:codepoints-to-string(84, 104, 233, 114, 232, 115, 101) 結果:'Thérèse' |
fn:string-to-codepoints(string) |
根據字串返回代碼點序列。 例子:string-to-codepoints("Thérèse") 結果:84, 104, 233, 114, 232, 115, 101 |
fn:codepoint-equal(comp1,comp2) | 根據 Unicode 代碼點對照,如果 comp1 的值等於 comp2 的值,則返回 true。(http://www.w3.org/2005/02/xpath-functions/collation/codepoint),否則返回 false。 |
|
如果 comp1 小於 comp2,則返回 -1。如果 comp1 等於 comp2,則返回 0。如果 comp1 大於 comp2,則返回 1。(根據所用的對照規則)。 例子:compare('ghi', 'ghi') 結果:0 |
fn:concat(string,string,...) |
返回字串的拼接。 例子:concat('XPath ','is ','FUN!') 結果:'XPath is FUN!' |
fn:string-join((string,string,...),sep) |
使用 sep 參數作為分隔符號,來返回 string 參數拼接後的字串。 例子:string-join(('We', 'are', 'having', 'fun!'), ' ') 結果:' We are having fun! ' 例子:string-join(('We', 'are', 'having', 'fun!')) 結果:'Wearehavingfun!' 例子:string-join((), 'sep') 結果:'' |
|
返回從 start 位置開始的指定長度的子字串。第一個字元的下標是 1。如果省略 len 參數,則返回從位置 start 到字串末尾的子字串。 例子:substring('Beatles',1,4) 結果:'Beat' 例子:substring('Beatles',2) 結果:'eatles' |
|
返回指定字串的長度。如果沒有 string 參數,則返回當前節點的字串值的長度。 例子:string-length('Beatles') 結果:7 |
|
刪除指定字串的開頭和結尾的空白,並把內部的所有空白序列替換為一個,然後返回結果。如果沒有 string 參數,則處理當前節點。 例子:normalize-space(' The XML ') 結果:'The XML' |
fn:normalize-unicode() | 執行 Unicode 規格化。 |
fn:upper-case(string) |
把 string 參數轉換為大寫。 例子:upper-case('The XML') 結果:'THE XML' |
fn:lower-case(string) |
把 string 參數轉換為小寫。 例子:lower-case('The XML') 結果:'the xml' |
fn:translate(string1,string2,string3) |
把 string1 中的 string2 替換為 string3。 例子:translate('12:30','30','45') 結果:'12:45' 例子:translate('12:30','03','54') 結果:'12:45' 例子:translate('12:30','0123','abcd') 結果:'bc:da' |
fn:escape-uri(stringURI,esc-res) |
例子:escape-uri("http://example.com/test#car", true()) 結果:"http%3A%2F%2Fexample.com%2Ftest#car" 例子:escape-uri("http://example.com/test#car", false()) 結果:"http://example.com/test#car" 例子:escape-uri ("http://example.com/~bébé", false()) 結果:"http://example.com/~b%C3%A9b%C3%A9" |
fn:contains(string1,string2) |
如果 string1 包含 string2,則返回 true,否則返回 false。 例子:contains('XML','XM') 結果:true |
fn:starts-with(string1,string2) |
如果 string1 以 string2 開始,則返回 true,否則返回 false。 例子:starts-with('XML','X') 結果:true |
fn:ends-with(string1,string2) |
如果 string1 以 string2 結尾,則返回 true,否則返回 false。 例子:ends-with('XML','X') 結果:false |
fn:substring-before(string1,string2) |
返回 string2 在 string1 中出現之前的子字串。 例子:substring-before('12/10','/') 結果:'12' |
fn:substring-after(string1,string2) |
返回 string2 在 string1 中出現之後的子字串。 例子:substring-after('12/10','/') 結果:'10' |
fn:matches(string,pattern) |
如果 string 參數匹配指定的模式,則返回 true,否則返回 false。 例子:matches("Merano", "ran") 結果:true |
fn:replace(string,pattern,replace) |
把指定的模式替換為 replace 參數,並返回結果。 例子:replace("Bella Italia", "l", "*") 結果:'Be**a Ita*ia' 例子:replace("Bella Italia", "l", "") 結果:'Bea Itaia' |
fn:tokenize(string,pattern) |
例子:tokenize("XPath is fun", "\s+") 結果:("XPath", "is", "fun") |
針對 anyURI 的函數
名稱 | 說明 |
---|---|
fn:resolve-uri(relative,base) |
關於布爾值的函數
名稱 | 說明 |
---|---|
fn:boolean(arg) | 返回數字、字串或節點集的布爾值。 |
fn:not(arg) |
首先通過 boolean() 函數把參數還原為一個布爾值。如果該布爾值為 false,則返回 true,否則返回 true。 例子:not(true()) 結果:false |
fn:true() |
返回布爾值 true。 例子:true() 結果:true |
fn:false() |
返回布爾值 false。 例子:false() 結果:false |
有關持續時間、日期和時間的函數
日期、時間、持續時間的組件提取函數
名稱 | 說明 |
---|---|
fn:dateTime(date,time) | 把參數轉換為日期和時間。 |
fn:years-from-duration(datetimedur) | 返回參數值的年份部分的整數,以標準辭彙表示法來表示。 |
fn:months-from-duration(datetimedur) | 返回參數值的月份部分的整數,以標準辭彙表示法來表示。 |
fn:days-from-duration(datetimedur) | 返回參數值的天部分的整數,以標準辭彙表示法來表示。 |
fn:hours-from-duration(datetimedur) | 返回參數值的小時部分的整數,以標準辭彙表示法來表示。 |
fn:minutes-from-duration(datetimedur) | 返回參數值的分鐘部分的整數,以標準辭彙表示法來表示。 |
fn:seconds-from-duration(datetimedur) | 返回參數值的分鐘部分的十進位數,以標準辭彙表示法來表示。 |
fn:year-from-dateTime(datetime) |
返回參數本地值的年部分的整數。 例子:year-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10")) 結果:2005 |
fn:month-from-dateTime(datetime) |
返回參數本地值的月部分的整數。 例子:month-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10")) 結果:01 |
fn:day-from-dateTime(datetime) |
返回參數本地值的天部分的整數。 例子:day-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10")) 結果:10 |
fn:hours-from-dateTime(datetime) |
返回參數本地值的小時部分的整數。 例子:hours-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10")) 結果:12 |
fn:minutes-from-dateTime(datetime) |
返回參數本地值的分鐘部分的整數。 例子:minutes-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10")) 結果:30 |
fn:seconds-from-dateTime(datetime) |
返回參數本地值的秒部分的十進位數。 例子:seconds-from-dateTime(xs:dateTime("2005-01-10T12:30:00-04:10")) 結果:0 |
fn:timezone-from-dateTime(datetime) | 返回參數的時區部分,如果存在。 |
fn:year-from-date(date) |
返回參數本地值中表示年的整數。 例子:year-from-date(xs:date("2005-04-23")) 結果:2005 |
fn:month-from-date(date) |
返回參數本地值中表示月的整數。 例子:month-from-date(xs:date("2005-04-23")) 結果:4 |
fn:day-from-date(date) |
返回參數本地值中表示天的整數。 例子:day-from-date(xs:date("2005-04-23")) 結果:23 |
fn:timezone-from-date(date) | 返回參數的時區部分,如果存在。 |
fn:hours-from-time(time) |
返回參數本地值中表示小時部分的整數。 例子:hours-from-time(xs:time("10:22:00")) 結果:10 |
fn:minutes-from-time(time) |
返回參數本地值中表示分鐘部分的整數。 例子:minutes-from-time(xs:time("10:22:00")) 結果:22 |
fn:seconds-from-time(time) |
返回參數本地值中表示秒部分的整數。 例子:seconds-from-time(xs:time("10:22:00")) 結果:0 |
fn:timezone-from-time(time) | 返回參數的時區部分,如果存在。 |
fn:adjust-dateTime-to-timezone(datetime,timezone) | 如果 timezone 參數為空,則返回沒有時區的 dateTime。否則返回帶有時區的 dateTime。 |
fn:adjust-date-to-timezone(date,timezone) | 如果 timezone 參數為空,則返回沒有時區的 date。否則返回帶有時區的 date。 |
fn:adjust-time-to-timezone(time,timezone) | 如果 timezone 參數為空,則返回沒有時區的 time。否則返回帶有時區的 time。 |
與 QNames 相關的函數
名稱 | 說明 |
---|---|
fn:QName() | |
fn:local-name-from-QName() | |
fn:namespace-uri-from-QName() | |
fn:namespace-uri-for-prefix() | |
fn:in-scope-prefixes() | |
fn:resolve-QName() |
關於節點的函數
名稱 | 說明 |
---|---|
|
返回當前節點的名稱或指定節點集中的第一個節點。 |
|
返回當前節點的名稱或指定節點集中的第一個節點 - 不帶有命名空間首碼。 |
|
返回當前節點或指定節點集中第一個節點的命名空間 URI。 |
fn:lang(lang) |
如果當前節點的語言匹配指定的語言,則返回 true。 例子:Lang("en") is true for <p xml:lang="en">...</p> 例子:Lang("de") is false for <p xml:lang="en">...</p> |
|
返回當前節點或指定的節點所屬的節點樹的根節點。通常是文檔節點。 |
有關序列的函數
一般性的函數
名稱 | 說明 |
---|---|
fn:index-of((item,item,...),searchitem) |
返回在專案序列中等於 searchitem 參數的位置。 例子:index-of ((15, 40, 25, 40, 10), 40) 結果:(2, 4) 例子:index-of (("a", "dog", "and", "a", "duck"), "a") Result (1, 4) 例子:index-of ((15, 40, 25, 40, 10), 18) 結果:() |
fn:remove((item,item,...),position) |
返回由 item 參數構造的新序列 - 同時刪除 position 參數指定的專案。 例子:remove(("ab", "cd", "ef"), 0) 結果:("ab", "cd", "ef") 例子:remove(("ab", "cd", "ef"), 1) 結果:("cd", "ef") 例子:remove(("ab", "cd", "ef"), 4) 結果:("ab", "cd", "ef") |
fn:empty(item,item,...) |
如果參數值是空序列,則返回 true,否則返回 false。 例子:empty(remove(("ab", "cd"), 1)) 結果:false |
fn:exists(item,item,...) |
如果參數值不是空序列,則返回 true,否則返回 false。 例子:exists(remove(("ab"), 1)) 結果:false |
fn:distinct-values((item,item,...),collation) |
返回唯一不同的值。 例子:distinct-values((1, 2, 3, 1, 2)) 結果:(1, 2, 3) |
fn:insert-before((item,item,...),pos,inserts) |
返回由 item 參數構造的新序列 - 同時在 pos 參數指定位置插入 inserts 參數的值。 例子:insert-before(("ab", "cd"), 0, "gh") 結果:("gh", "ab", "cd") 例子:insert-before(("ab", "cd"), 1, "gh") 結果:("gh", "ab", "cd") 例子:insert-before(("ab", "cd"), 2, "gh") 結果:("ab", "gh", "cd") 例子:insert-before(("ab", "cd"), 5, "gh") 結果:("ab", "cd", "gh") |
fn:reverse((item,item,...)) |
返回指定的專案的顛倒順序。 例子:reverse(("ab", "cd", "ef")) 結果:("ef", "cd", "ab") 例子:reverse(("ab")) 結果:("ab") |
fn:subsequence((item,item,...),start,len) |
返回 start 參數指定的位置返回專案序列,序列的長度由 len 參數指定。第一個專案的位置是 1。 例子:subsequence(($item1, $item2, $item3,...), 3) 結果:($item3, ...) 例子:subsequence(($item1, $item2, $item3, ...), 2, 2) 結果:($item2, $item3) |
fn:unordered((item,item,...)) | 依據實現決定的順序來返回專案。 |
測試序列容量的函數
名稱 | 說明 |
---|---|
fn:zero-or-one(item,item,...) | 如果參數包含零個或一個專案,則返回參數,否則生成錯誤。 |
fn:one-or-more(item,item,...) | 如果參數包含一個或多個專案,則返回參數,否則生成錯誤。 |
fn:exactly-one(item,item,...) | 如果參數包含一個專案,則返回參數,否則生成錯誤。 |
Equals, Union, Intersection and Except
名稱 | 說明 |
---|---|
fn:deep-equal(param1,param2,collation) | 如果 param1 和 param2 與彼此相等(deep-equal),則返回 true,否則返回 false。 |
合計函數
名稱 | 說明 |
---|---|
fn:count((item,item,...)) | 返回節點的數量。 |
fn:avg((arg,arg,...)) | 返回參數值的平均數。 例子:avg((1,2,3)) 結果:2 |
fn:max((arg,arg,...)) | 返回大於其他參數的參數。 例子:max((1,2,3)) 結果:3 例子:max(('a', 'k')) 結果:'k' |
fn:min((arg,arg,...)) | 返回小於其他參數的參數。 例子:min((1,2,3)) 結果:1 例子:min(('a', 'k')) 結果:'a' |
fn:sum(arg,arg,...) | 返回指定節點集中每個節點的數值的總和。 |
生成序列的函數
名稱 | 說明 |
---|---|
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) | 如果 doc() 函數返回文檔節點,則返回 true,否則返回 false。 |
|
上下文函數
名稱 | 說明 |
---|---|
fn:position() |
返回當前正在被處理的節點的 index 位置。 例子://book[position()<=3] 結果:選擇前三個 book 元素 |
fn:last() |
返回在被處理的節點列表中的專案數目。 例子://book[last()] 結果:選擇最後一個 book 元素 |
fn:current-dateTime() | 返回當前的 dateTime(帶有時區)。 |
fn:current-date() | 返回當前的日期(帶有時區)。 |
fn:current-time() | 返回當前的時間(帶有時區)。 |
fn:implicit-timezone() | 返回隱式時區的值。 |
fn:default-collation() | 返回默認對照的值。 |
fn:static-base-uri() | 返回 base-uri 的值。 |