XPath 節點
XPath 術語
節點
在 XPath 中,有七種型別的節點:元素、屬性、文字、名稱空間、處理指令、註釋和根節點。
XML 文件被視為節點的樹。樹的最頂層元素稱為根元素。
檢視以下 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<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"
項
項是原子值或節點。
節點關係
父節點
每個元素和屬性都有一個父節點。
在以下示例中;book 元素是 title、author、year 和 price 的父節點
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
子節點
元素節點可以有零個、一個或多個子節點。
在以下示例中;title、author、year 和 price 元素都是 book 元素的子節點
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
同級節點
具有相同父節點的節點。
在以下示例中;title、author、year 和 price 元素都是同級節點
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
祖先節點
節點的父節點、父節點的父節點等。
在以下示例中;title 元素的祖先節點是 book 元素和 bookstore 元素
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
後代節點
節點的子節點、子節點的子節點等。
在以下示例中;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>