Latest web development tutorials

XPath、XQuery 以及XSLT 函數

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


函數參考手冊

Note 函數命名空間的默認前綴為fn:

Note 函數命名空間的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)

結果: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)

結果:'Therese'

fn:string-to-codepoints(string)

根據字符串返回代碼點序列。

例子:string-to-codepoints("Therese")

結果:84, 104, 233, 114, 232, 115, 101

fn:codepoint-equal(comp1,comp2) 根據Unicode 代碼點對照,如果comp1 的值等於comp2 的值,則返回true。 (http://www.w3.org/2005/02/xpath-functions/collat​​ion/codepoint),否則返回false。
  • fn:compare(comp1,comp2)
  • fn:compare(comp1,comp2,collat​​ion)

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

結果:''

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

結果:'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/~bebe", 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()

關於節點的函數

名稱 說明
  • 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 <p xml:lang="en">...</p>

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

結果:("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,...),collat​​ion)

返回唯一不同的值。

例子: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,collat​​ion) 如果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: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-collat​​ion() 返回默認對照的值。
fn:static-base-uri() 返回base-uri 的值。