xpath

XPath 簡介

什麼是 XPath?
XPath 使用路徑表達式在 XML 文檔中進行導航
XPath 包含一個標準函數庫
XPath 是 XSLT 中的主要元素
XPath 是一個 W3C 標準

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)
元素節點可有零個、一個或多個子。
在下面的例子中,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>

發佈了66 篇原創文章 · 獲贊 31 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章