+ All Categories
Home > Technology > Peggy - XPath

Peggy - XPath

Date post: 11-Apr-2017
Category:
Upload: learningtech
View: 385 times
Download: 0 times
Share this document with a friend
14
XPath Peggy 2015/6/26
Transcript
Page 1: Peggy - XPath

XPathPeggy2015/6/26

Page 2: Peggy - XPath

Introduction

XML (eXtensible Markup Language) XSL = Style Sheets for XML XSL - More Than a Style Sheet Language

XSLT (eXtensible Style Language Transform) Transform one XML to another XML or other text such as HTML

XPath (XML Path) A way to locate items in a XML file

XSL-FO A language for formatting XML documents

Page 3: Peggy - XPath

XPath

What is XPath? XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is also used in XQuery, XPointer and XLink XPath is a W3C recommendation

XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

Page 4: Peggy - XPath

XQuery

XQuery XQuery 和 XML 資料查詢有關。 XQuery 被設計用來查詢任何可作為 XML 形態呈現的資料,包括資料庫。

for $x in doc(“books.xml”)/bookstore/book //doc(“books.xml”) 開啟文件where $x/price>30

order by $x/title

return $x/title

Page 5: Peggy - XPath

XPointer and XLink

XLink 定義了一套標準的在 XML 文檔中創建超級連結的方法。 <homepage xlink:type="simple" xlink:href="http://www.w3.org">Visit

W3C</homepage> XPointer

使超級連結可以指向 XML 文檔中更多具體的部分(片斷)。 href="http://www.example.com/cdlist.xml#id('rock').child(5,item)"

Page 6: Peggy - XPath

Compare

Page 7: Peggy - XPath

XPath Relationship of Nodes

Parent the book element is the parent of the title and price

Children the title and price elements are all children of the book element

Siblings  the title, author, year, and price elements are all siblings

Ancestors the ancestors of the title element are the book element and the bookstore element

Descendants descendants of the bookstore element are the book, title and price elements

<bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>

Page 8: Peggy - XPath

Selecting Nodes

運算式 描述nodename 選取此節點的所有子節點。/ 從根節點選取。// 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。. 選取當前節點。.. 選取當前節點的父節點。@ 選取屬性。

Page 9: Peggy - XPath

Selecting Nodes - Example

路徑運算式 結果bookstore 選取 bookstore 元素的所有子節點。/bookstore 選取根項目 bookstore 。

注釋:假如路徑起始於正斜杠 ( / ) ,則此路徑始終代表到某元素的絕對路徑!bookstore/book 選取屬於 bookstore 的子元素的所有 book 元素。//book 選取所有 book 子元素,而不管它們在文檔中的位置。bookstore//book 選擇屬於 bookstore 元素的後代的所有 book 元素,而不管它們位於

bookstore 之下的什麼位置。//@lang 選取名為 lang 的所有屬性。

<bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>

Page 10: Peggy - XPath

Predicates - Example

路徑運算式 結果/bookstore/book[1]

選取屬於 bookstore 子元素的第一個 book 元素。Note: In IE 5,6,7,8,9 first node is[0], but according to W3C, it is [1].

/bookstore/book[last()] 選取屬於 bookstore 子元素的最後一個 book 元素。/bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素。/bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元

素。//title[@lang] 選取所有擁有名為 lang 的屬性的 title 元素。//title[@lang='eng'] 選取所有 title 元素,且這些元素擁有值為 eng 的 lang 屬性。/bookstore/book[price>35.00] 選取 bookstore 元素的所有 book 元素,且其中的 price 元

素的值須大於 35.00 。/bookstore/book[price>35.00]/title

選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大於 35.00 。

Page 11: Peggy - XPath

Selecting Unknown Nodes

萬用字元 描述* 匹配任何元素節點。@* 匹配任何屬性節點。node() 匹配任何類型的節點。

Page 12: Peggy - XPath

Selecting Unknown Nodes - Example

路徑運算式 結果/bookstore/* 選取 bookstore 元素的所有子元素。//* 選取文檔中的所有元素。//title[@*] 選取所有帶有屬性的 title 元素。

Page 13: Peggy - XPath

Selecting Several Paths - Example

路徑運算式 結果//book/title | /book/price 選取 book 元素的所有 title 和 price 元素。//title | //price 選取文檔中的所有 title 和 price 元素。/bookstore/book/title | //price

選取屬於 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。

Page 14: Peggy - XPath

XPath Axes

Location Path Expression An absolute location path: /step/step/... A relative location path: step/step/...

Each step is evaluated against the nodes in the current node-set. A step consists of:

an axis (defines the tree-relationship between the selected nodes and the current node)

a node-test (identifies a node within an axis) zero or more predicates (to further refine the selected node-set)


Recommended