XPath ノード

在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档节点(或称为根节点)。

XPath 术语

节点(Node)

在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。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" (属性节点) 

基本值(或称原子值,Atomic value)

基本值是无父或无子的节点。

基本值的例子:

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>