XPath リファレンス
XModify と XPath を使い、ドキュメントを修正することができます。XPath はXModify 文、XModify サーバーレスポンス、マクロ、ドキュメントデータバインディングを用いた Nexaweb アプリケーション開発に重要なものです。
概要
XML 文書を修正することが、XPath の主な目的です。XPat hは、XML 文書の表面的な構文ではなく XML 文書の抽象的な倫理構造の上で処理を行います。XPath という名称は、XML 文書の階層構造をたどる際に URL と同様のパス表記法を採用している所から命名さ
ノードツリー
Xpath はXML文書をツリーとして扱います。
ツリーは次の型のノードを持っています :
- Root
- Element
- Text
- Attribute
- Namespace
- Processing instruction
- Comment
各ノードタイプにおいて、文字列を決定する方法があります。文字列値がそのノードの一部となるノード型もあれば、子孫ノードの文字列値から算出されるノード型もあります。
一部のノード型は、ローカル部分と名前空間 URI でペア構成されている拡張名を持っています。
| ノード型 | 説明 |
| Root | ツリーのルートのみ。文書要素の要素ノードはルートノードの子ノードになる。 拡張名: N/A文字列値: ルートノードの全ての子孫テキストノードの文字列値を文書順に連結したもの。 |
| Element | 文書上、各要素にひとつ 拡張名: tagに指定した要素のクオリファイドネーム(QName)をXML名前空間勧告[XML名]に従い拡張することによって得られる。Qnameに接頭辞がなく、かつ、適用し得る既定の名前空間が無い場合、要素の拡張名の名前空間URIはnullになる。 文字列値: 要素ノードの全ての子孫テキストノードの文字列値を文書順に連結したもの。 |
| Attribute | 各要素ノードは、関連する属性ノードの集合を持っている。要素はそれらの属性ノードの親ノードになる。しかしながら、属性ノードはその親要素ノードの子ノードにはならない。 拡張名: tag に指定したQName をXML 名前空間勧告 [XML名] に従い拡張することによって得られる。属性のQName に接頭辞がない場合、属性の名前の名前空間URI が null になる。 文字列値: XML 勧告 [XML] で規定された正規化された値. 正規化された値がゼロ長文字の属性になっても特別扱いされない。結果文字列値がゼロ長文字の属性ノードにとして扱われる。 |
| Namespace | 各要素は対応づけられた名前空間ノードの集合を持っており、1つは要素のスコープに入る規定の名前空拳に対応し、残りは要素のスコープに入る異なる名前空間接頭辞に1つづつ対応するものになる。要素はこれらの各名前空間ノードの親ノードであるが、名前空間ノードは親要素ノードの子ノードにはならない。次に挙げるものに対応する名前空間ノードを持つことになる :
拡張名: namespace 接頭語であるローカル部分(名前空間ノードがデフォルト名前空間の場合空白)とnamespace URI は常にnull 文字列値: namespace 接頭語になる。 namespace URI 関連していれば、拡張名でnamespace URIとして解決できる。 |
| Processing Instruction | 文書型宣言内に説明されるものを除き、すべての処理命令には対応する処理命令ノードが存在する。 拡張名: 局所部は処理命令の処理対象になる : 名前空間 URI はnull になる。 処理命令ノードの文字列値は、処理命令のうち処理対象と空白に後続する部分になる。 |
| Comment | 文書型宣言内に説明されるコメントを除き、すべてのコメントには対応するコメントノードが存在する 拡張名: N/A 文字列値: コメントの文字列値はコメントの内容である。先頭の <!— と末尾の --> は内容に含まれない |
Document Order
ツリーは以下のように、文書上全てのノードが定義されている文書順を含みます :
- The root node is the first node.
- Element nodes occur in order of their start-tag in the XML (after expansion of entities).
- Element nodes occur before their children.
- Attribute and namespace nodes of an element occur before the children of the element.
- Namespace nodes occur before attribute nodes.
- The relative order of namespace and attribute nodes is implementation-dependent.
ルートノードと要素ノードは以下のように順序づけられた子ノードのリストを持っている :
- Nodes never share children.
- Every node other than the root node has exactly one parent, which is either an element node or the root node.
- A root node or an element node is the parent of each of its child nodes.
- Descendants of a node are the children of the node and the descendants of the children of the node.
ロケーションパス
| パスのタイプ | 説明 |
| Absolute location path | / とオプションで後続する相対ロケーションパスで構成される。 / 自体は文脈ノードを含んでいる文書のルートノードを選択する。相対ロケーションパスを後続させた場合、そのロケーションパスは、文脈ノードを含む文書のルートノードに対し相対ロケーションパスにより選択されたノード集合体を選択する。 |
| Relative location path | /で区切られた1つ以上のロケーションステップを順に並べたものから構成される。各ステップは左から右に結合され、それぞれが文脈ノードに対して相対的なノードの集合を順次選択する。 各ステップの最初のシーケンスでは、文脈ノードに対して相対的なノードの集合を選択する。 その集合に属する各ノードは、後続のステップの文脈ノードに使用される。後段のステップで特定されたノードの集合は1つに併合され、この併合されたノード集合が、ステップの結合により特定されるノード集合となる。例えば child::div/child::para は文脈ノードの div 子要素の para 子要素を選択する。言い換えれば div 要素を親に持つ para 孫要素を選択する。 |
ロケーションステップは次の3つの部分からなる :
- 軸はロケーションステップにより選択されるノードと、文脈ノードとの間のツリー関係を指定する
- ノードテストはロケーションステップで選択されるノードのノード型と拡張名を指定する
- 0 個以上の述語。ロケーションステップで選択されるノードの集合を、任意の式により更に選別する
ロケーションパス 省略構文
| 構文 | 選択 |
| Para | 文脈ノードの para 要素の子 |
| * | 文脈ノードの子要素すべて |
| Text() | 文脈ノードの子テキストノードすべて |
| @name | 文脈ノードの name 属性 |
| @* | 文脈ノードの属性すべて |
| Para[1] | 文脈ノードの最初の para 子要素 |
| para[last()] | 文脈ノードの最後の para 子要素 |
| */para | 文脈ノードのpara孫要素すべて |
| /doc/chapter[5]/section[2] | doc 子要素(文書要素)の5番目の chapter 子要素の2番目の section 子要素 |
| chapter//para | 文脈ノードのchapter子要素の para 子孫要素 |
| //para | 文書ルートのpara子孫要素すべて 文脈ノードとして、同じ文書内para要素すべて |
| .. | 文脈ノードの親ノード |
| ../@lang | 文脈ノードの親ノードのlang属性 |
| para[@type="warning"] | 文脈ノードの para 子要素のうち、type 属性の値がwarning になるものすべて |
| para[@type="warning"][5] | Para子要素のうち、type 属性の値が warning になるものの中から5番目のもの |
| para[5][@type="warning"] | 文脈ノードの5番目のpara子要素のtype属性の値がwarningならば、その子要素 |
| chapter[title="Introduction"] | 文脈ノードのchapter子要素のうち、文字列値がIntroductionになるtitle子要素を1個以上もつものすべて |
| chapter[title] | 文脈ノードの chapter 子要素のうち、title子要素を1個以上持つものすべて |
| employee[@secretary and @assistant] | 文脈ノードのemployee 子要素のうち、secretary 属性と assistant 属性の両方を持つものすべて |
一般的な略称ガイドライン
| 構文要素 | 説明 |
| Child | デフォルト軸; つまりロケーションステップから省略することが可能 div/para = child::div/child::para |
| @ | Attributeの省略形 例: para[@type="warning"] はchild::para[attribute::type="warning"] を省略したものになる それぞれwarning と同等の値を持ったtype属性のあるpara の子を選択する |
| // | : /descendant-or-self::node()の省略形 例: //para = /descendant-or-self::node()/child::para それぞれ文書上でpara 要素を選択する (文書要素であるpara 要素ですら) |
| div//para | Short for: div/descendant-or-self::node()/child::para. それぞれが div の子の全ての para 子孫を選択する |
| . | Short for: self::node. Useful in conjuction with //. For example: .// = self::node()/descendant-or-self node()/child::para それぞれは文脈ノードの全ての para 子孫を選択する |
| .. | Short for: parent::node() For example: ../title = parent::node()/child::title それぞれ文脈ノードの親の title の子を選択する |
軸
このセクションでは軸の種類一覧とパスの特定時、利用可能な軸の説明をしています。
軸のタイプ
| タイプ | 説明 |
| Forward Axis | 文書オーダーで前にある文脈ノード、またノードのみを含む |
| Reverse Axis | 文書オーダーで後にある文脈ノード、またノードのみを含む |
軸のリスト
| 軸 | 説明 | タイプ |
| ancestor | 文脈ノードの祖先ノードすべてを選択する。文脈ノードの祖先ノードとは、文脈ノードの親ノード、親ノードの親ノード、・・・などである。したがって、文脈ノードがルートノードの場合を除き、ancestor 軸には常にルートノードも含まれることになる。 | Reverse |
| ancestor-or-self | 文脈ノードと文脈ノードの祖先を含む。したがって祖先軸は常にルートノードも含まれる。 | Reverse |
| attribute | 文脈ノードの属性を含む。この軸は、文脈ノードが要素でない限り空になる。 | Forward |
| child | 文脈ノードの子を含む | Forward |
| descendant | 文脈ノードの子孫ノードをすべて選択する。子孫とは子ノード、子ノードの子ノードなどであるため、descendant 軸に属性ノードまたは名前空間ノードを含まない。 | Forward |
| descendant-or-self | 文脈ノードと文脈ノードの子孫ノード全てを含む。 | Forward |
| following | 文脈ノードと同じ文書内で文書順で文脈ノード以降にあるノードすべてを選択する。ただし子孫ノード、属性ノード、名前空間ノードは除外される。 | Forward |
| following-sibling | 文脈ノードに後続する同胞ノード全てを含む。文脈ノードが属性ノードまたは空間ノードの場合、following-sibling 軸は空になる。 | Forward |
| namespace | 文脈ノードの名前空間ノードを含む。軸が空でない限り、文脈ノードは要素である。 | Forward |
| parent | 文脈ノードの親ノードが存在するならば選択する | Forward |
| preceding | 文書内で文書順で文脈ノードより前にあるノードすべてを選択する。ただし先祖ノード、属性ノード、名前空間ノードは除外される | Reverse |
| preceding-sibling | 文脈ノードが属性もしくは名前空間ノードの場合(その場合は空)を除き、文脈ノードの全てのpreceding siblings を含む。 | Reverse |
| self | 文脈ノードのみを含む。. | Forward |
どの軸にも、主ノード型を持っています。軸が要素を選択する場合、主ノード型は要素です。それ以外は、軸が選択するノードの型が主ノード型になります。
| 軸 | 主要ノードタイプ |
| attribute | attribute |
| namespace | namespace |
| All other axes | element |
ノードテスト
ノードタイプとロケーションステップで選択されたノードの拡張名を特定します。
初期のノードセットは一つ目の述語によってふるいにかけられ、新しいノード集合が生成されます。この新たなノード集合は2番目の述語を使用することにその他により、更にふるいにかけられます。最終ノード集合は、ロケーションステップによって選択されるノード集合になります。
ノードテスト*は、主ノード型のノードであればどのノードでも”真”になる
例 :
| ノードテスト | 説明 |
| child::* | 文脈ノードの全ての子要素を選択する |
| attribute::* | 文脈ノードの全ての属性を選択する |
ノードテストtext() は全てのテキストノードに対しても真になる
例 :
| ノードテスト | 説明 |
| child:: text() | 文脈ノードの子テキストノードを選択する |
| attribute::text() | 文脈ノードの全てのattributeを選択する |
ノードテストコメント()は全てのコメントノードに対し真になる。
ノードテスト processing-instruction() もすべての処理命令に対し真になる。
このノードテストはLiteral である引数をとり得ます。この場合、Literal の値と同じ名前を持っている処理命令すべてに対し真です。
述部
位置 :
| 位置 | 説明 |
| Forward axis | 文書順上でノード集合内のノードの位置 |
| Reverse Axis | 逆文書順でノード集合以内のノードの位置 |
- First position = 1
- Para[3] is equivalent to para[position()=3]
関数
この節では、全ての XPath 実装サポートをするコア関数ライブラリの一覧を示しています。それぞれの関数は0個以上の引数をとり、1 個の結果を返します。
コア関数ライブラリに属する関数については、引数や結果の帰り値は上の4つの基本型のいずれかになる :
- node-set (an unordered collection of nodes without duplicates)
- boolean (true or false)
- number (a floating-point number)
- string (a sequence of UCS characters)
| Function Type | Function | Syntax | Description |
| Node Set | last | number last() | Returns a number equal to the context size from the expression evaluation context. |
| position | number position() | Returns a number equal to the context position from the expression evaluation context. | |
| count | number count(node-set) | Returns the number of nodes in the argument node-set. | |
| id | node-set id(object) | Selects elements by their unique ID. For example: | |
| local-name | string local-name(node-set?) | Returns the local part of the expanded-name of the node in the argument node-set that is first in document order. | |
| namespace-uri | string namespace-uri(node-set?) | Returns the namespace URI of the expanded-name of the node in the argument node-set that is first in document order. | |
| name | string name(node-set?) | Returns a string containing a qualified name (QName) representing the expanded-name of the node in the argument node-set that is first in document order. | |
| String | string | string string(object?) | Converts an object to a string. |
| concat | string concat(string, string, string*) | Returns the concatenation of its arguments. | |
| starts-with | boolean starts-with(string, string) | Returns true if the first argument string starts with the second argument string, and otherwise returns false. | |
| contains | boolean contains(string, string) | Returns true if the first argument string contains the second argument string, and otherwise returns false. | |
| substring-before | string substring-before(string, string) | Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. For example, substring-before("1999/04/01","/") returns 1999. | |
| substring-after | string substring-after(string, string) | Returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. For example, substring-after("1999/04/01","/") returns 04/01, and substring-after("1999/04/01","19") returns 99/04/01. | |
| substring | string substring(string, number, number?) | Returns the substring of the first argument starting at the position specified in the second argument with length specified in the third argument. For example, substring("12345",2,3) returns "234". If the third argument is not specified, it returns the substring starting at the position specified in the second argument and continuing to the end of the string. For example, substring("12345",2) returns "2345". | |
| string-length | number string-length(string?) | Returns the number of characters in the string. | |
| normalize space | string normalize-space(string?) | Returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. | |
| translate | string translate(string, string, string) | Returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. For example, translate("bar","abc","ABC") returns the string BAr. | |
| Boolean | boolean | boolean boolean(object) | Converts its argument to a boolean as follows:
|
| not | boolean not(boolean) | Returns true if its argument is false, and false otherwise. | |
| true | boolean true() | Returns true. | |
| false | boolean false() | Returns false. | |
| lang | boolean lang(string) | Returns true or false depending on whether the language of the context node as specified by xml:lang attributes is the same as or is a sublanguage of the language specified by the argument string. | |
| Number | number | number number(object?) | Converts its argument to a number as follows:
|
| sum | number sum(node-set) | Returns the sum, for each node in the argument node-set, of the result of converting the string-values of the node to a number. | |
| floor | number floor(number) | Returns the largest (closest to positive infinity) number that is not greater than the argument and that is an integer. | |
| ceiling | number ceiling(number) | Returns the smallest (closest to negative infinity) number that is not less than the argument and that is an integer. | |
| round | number round(number) | Returns the number that is closest to the argument and that is an integer. If there are two such numbers, then the one that is closest to positive infinity is returned. If the argument is:
|