XPathノード

XPathでは、7種類のノードがあります:要素、属性、テキスト、名前空間、処理指令、コメントおよびドキュメントノード(またはルートノードと呼ばれる)です。

XPathの用語

ノード(Node)

XPathでは、7種類のノードがあります:要素、属性、テキスト、名前空間、処理指令、コメントおよびドキュメント(ルート)ノードです。XMLドキュメントはノードの木として扱われます。木のルートはドキュメントノードまたはルートノードと呼ばれます。

以下のXMLドキュメントを見てください:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author> 
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>

上記のXMLドキュメントのノードの例:

<bookstore> (ドキュメントノード)
<author>J K. Rowling</author> (要素ノード)
lang="en" (属性ノード) 

基本値(または原子値と呼ばれる)

基本値は親も子も持っていないノードです。

基本値の例:

J K. Rowling
"en"

プロジェクト(Item)

プロジェクトは基本値またはノードです。

ノード関係

親(Parent)

各要素および属性には親があります。

以下の例では、book要素はtitle、author、yearおよびprice要素の親です:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

子(Children)

要素ノードは子を持つことができるのは0個、1個、または複数です。

以下の例では、title、author、yearおよびprice要素はbook要素の子です:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

兄弟(Sibling)

同じ親を持つノード

以下の例では、title、author、yearおよびprice要素は兄弟です:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

祖先(Ancestor)

あるノードの親、親の親、など。

以下の例では、title要素の祖先はbook要素およびbookstore要素です:

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>

後裔(Descendant)

あるノードの子、孫、など。

以下の例では、bookstoreの後裔はbook、title、author、yearおよびprice要素です:

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>