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) クエリのデバッグに使用します。

数値に関する関数

名称 説明
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 を返します。
  • fn:compare(comp1,comp2)
  • fn:compare(comp1,comp2,collation)

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

指定された文字列の先頭と末尾の空白を削除し、内部のすべての空白シーケンスを1つに置き換えて、結果を返します。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 の場合は false を返します。

例: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) パラメータの値の分の部分を、標準的な語彙表現で表した10進数で返します。
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)

パラメータのローカル値の秒の部分を10進数で返します。

例: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") は次の場合に true です。 <p xml:lang="en">...</p>

例:Lang("de") は次の場合に false です。 <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,...),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,...) パラメータがゼロまたは1つのアイテムを含む場合はパラメータを返し、それ以外の場合はエラーを生成します。
fn:one-or-more(item,item,...) パラメータが1つ以上のアイテムを含む場合はパラメータを返し、それ以外の場合はエラーを生成します。
fn:exactly-one(item,item,...) パラメータが1つのアイテムを含む場合はパラメータを返し、それ以外の場合はエラーを生成します。

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:collection()
  • fn:collection(string)
 

コンテキスト関数

名称 説明
fn:position()

現在処理中のノードの index 位置を返します。

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

結果:最初の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 の値を返します。
その他の拡張