XPath ノード
XPath 用語
ノード
XPath では、ノードには7つのタイプがあります: 要素、属性、テキスト、名前空間、処理命令、コメント、およびドキュメント(ルート)ノードです。XML ドキュメントはノードツリーとして扱われます。ツリーのルートはドキュメントノードまたはルートノードと呼ばれます。
次の XML ドキュメントを見てください:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
Harry Potter
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
<bookstore>
<book>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
上記のXMLドキュメント内のノードの例:
<bookstore> (ドキュメントノード)
<author>J K. Rowling</author> (要素ノード)
lang="en" (属性ノード)
<author>J K. Rowling</author> (要素ノード)
lang="en" (属性ノード)
基本値(またはアトミック値、Atomic value)
基本値は、親も子もないノードです。
基本値の例:
J K. Rowling
"en"
"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>
<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>
<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>
<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>
<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>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>