天天看點

XPath、XQuery 以及 XSLT 函數函數參考手冊函數參考手冊

下面的參考手冊定義了XPath 2.0,XQuery 1.0和XSLT 2.0中的函數。

存取

錯誤和跟蹤

數值

字元串

AnyURI

邏輯

持續時間/日期/時間

QName

節點

序列

Context

XPath、XQuery 以及 XSLT 函數函數參考手冊函數參考手冊

函數命名空間的預設字首為 fn:

XPath、XQuery 以及 XSLT 函數函數參考手冊函數參考手冊

函數命名空間的 URI為 : http://www.w3.org/2005/xpath-functions

名稱

說明

fn:node-name(node)

傳回參數節點的節點名稱。

fn:nilled(node)

傳回是否拒絕參數節點的布爾值。

fn:data(item.item,...)

接受項目序列,并傳回原子值序列。

fn:base-uri()

fn:base-uri(node)

傳回目前節點或指定節點的 base-uri 屬性的值。

fn:document-uri(node)

傳回指定節點的 document-uri 屬性的值。

fn:error()

fn:error(error)

fn:error(error,description)

fn:error(error,description,error-object)

例子: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)

fn:ceiling(num)

傳回大于 num 參數的最小整數。

例子:ceiling(3.14)

結果:4

fn:floor(num)

傳回不大于 num 參數的最大整數。

例子:floor(3.14)

結果:3

fn:round(num)

把 num 參數舍入為最接近的整數。

例子:round(3.14)

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)

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。

fn:compare(comp1,comp2)

fn:compare(comp1,comp2,collation)

如果 comp1 小于 comp2,則傳回 -1。如果 comp1 等于 comp2,則傳回 0。如果 comp1 大于 comp2,則傳回 1。(根據所用的對照規則)。

例子:compare('ghi', 'ghi')

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

結果:''

fn:substring(string,start,len)

fn:substring(string,start)

傳回從 start 位置開始的指定長度的子字元串。第一個字元的下标是 1。如果省略 len 參數,則傳回從位置 start 到字元串末尾的子字元串。

例子:substring('Beatles',1,4)

結果:'Beat'

例子:substring('Beatles',2)

結果:'eatles'

fn:string-length(string)

fn:string-length()

傳回指定字元串的長度。如果沒有 string 參數,則傳回目前節點的字元串值的長度。

例子:string-length('Beatles')

結果:7

fn:normalize-space(string)

fn:normalize-space()

删除指定字元串的開頭和結尾的空白,并把内部的所有空白序列替換為一個,然後傳回結果。如果沒有 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')

例子: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')

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

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

fn:resolve-uri(relative,base)

fn:boolean(arg)

傳回數字、字元串或節點集的布爾值。

fn:not(arg)

首先通過 boolean() 函數把參數還原為一個布爾值。如果該布爾值為 false,則傳回 true,否則傳回 true。

例子:not(true())

fn:true()

傳回布爾值 true。

例子:true()

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

fn:timezone-from-dateTime(datetime)

傳回參數的時區部分,如果存在。

fn:year-from-date(date)

傳回參數本地值中表示年的整數。

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

fn:month-from-date(date)

傳回參數本地值中表示月的整數。

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

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

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

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。

fn:QName()

fn:local-name-from-QName()

fn:namespace-uri-from-QName()

fn:namespace-uri-for-prefix()

fn:in-scope-prefixes()

fn:resolve-QName()

fn:name()

fn:name(nodeset)

傳回目前節點的名稱或指定節點集中的第一個節點。

fn:local-name()

fn:local-name(nodeset)

傳回目前節點的名稱或指定節點集中的第一個節點 - 不帶有命名空間字首。

fn:namespace-uri()

fn:namespace-uri(nodeset)

傳回目前節點或指定節點集中第一個節點的命名空間 URI。

fn:lang(lang)

如果目前節點的語言比對指定的語言,則傳回 true。

例子:Lang("en") is true for

<p xml:lang="en">...</p>

例子:Lang("de") is false for

fn:root()

fn:root(node)

傳回目前節點或指定的節點所屬的節點樹的根節點。通常是文檔節點。

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)

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

如果參數值是空序列,則傳回 true,否則傳回 false。

例子:empty(remove(("ab", "cd"), 1))

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

如果參數值不是空序列,則傳回 true,否則傳回 false。

例子:exists(remove(("ab"), 1))

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

例子: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,...)

如果參數包含一個項目,則傳回參數,否則生成錯誤。

fn:deep-equal(param1,param2,collation)

如果 param1 和 param2 與彼此相等(deep-equal),則傳回 true,否則傳回 false。

fn:count((item,item,...))

傳回節點的數量。

fn:avg((arg,arg,...))

傳回參數值的平均數。

例子:avg((1,2,3))

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

傳回大于其它參數的參數。

例子:max((1,2,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:collection()

fn:collection(string)

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 的值。