Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

Similar presentations


Presentation on theme: "1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through."— Presentation transcript:

1 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through elements and attributes in an XML document

2 2 XPath XPath uses path expressions to select nodes or node- sets in an XML document. XPath includes over 100 built-in functions XML document is treated as a tree of nodes there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. Harry Potter J K. Rowling 2005 29.99 Atomic values are nodes with no children or parent.

3 3 Relationship of Nodes Parent Children Siblings Ancestors Descendants Harry Potter J K. Rowling 2005 29.99

4 4 Relationship of Nodes Parent Children Siblings Ancestors Descendants Harry Potter J K. Rowling 2005 29.99

5 5 Relationship of Nodes Parent Children Siblings Ancestors Descendants Harry Potter J K. Rowling 2005 29.99

6 6 Xpath Syntax

7 7 Sample xml document Harry Potter 29.99 Learning XML 39.95

8 Xpath Expressions 8

9 9 XPath Syntax Predicates –Predicates are used to find a specific node or a node that contains a specific value. –Predicates are always embedded in square brackets. –Example Selects the first book element that is the child of the bookstore element. –/bookstore/book[1]

10 10 Predicate (examples)

11 11 Predicate (examples)

12 12 Predicate (examples) Selecting Unknown Nodes –XPath wildcards can be used to select unknown XML elements.

13 13 Predicate (examples) /bookstore/* –Selects all child nodes of the bookstore element //* –Selects all elements in the document //title[@*] –Select all title element which have any attribute

14 14 XPath Syntax Selecting Several Paths –By using the | operator in an XPath Example –//book/title | //book/price Selects all the title And price elements of all book elements

15 15 XPath Syntax //title | //price –Selects all the title And price elements in the documents /bookstore/book/title | //price –Selects all the title elements of the book element of the bookstore element And all the price elements in the document

16 16 Location Path Expression An absolute location path: /step/step/... A relative location path: step/step/... –Where 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)

17 17 Location Path Expression (cont.) The syntax for a location step is: –axisname::nodetest [ predicate ] Example –child::book Selects all book nodes that are children of the current node –child::* Selects all children of the current node –attribute::* Selects all attributes of the current node

18 18 Location Path Expression (cont.) child::text() –Selects all text child nodes of the current node child::node() –Selects all child nodes of the current node descendant::book –Selects all book descendants of the current node ancestor::book –Selects all book ancestors of the current node

19 19 XPath Operators An XPath expression returns either a node-set, a string, a Boolean, or a number. Examples –Operator | //book | //cd Returns a node-set with all book and cd elements

20 20 XPath Operators –Other Operators +, -, *, div =, !=,,>=,<= or (logical or) and (logical and) mod (division remainder)

21 21 XPath Operators (Example) Or –price=9.80 or price=9.70 true if price is 9.80 false if price is 9.50 and (logical and) –price>9.00 and price<9.90 true if price is 9.80 false if price is 8.50

22 22 XPath Core Library Function –Node Set Functions Last (),Position (), Count () –String Functions Concat, contains, start-with, length

23 23 XPath Exercise

24 24 An XML Example Address all author elements Address the location attribute nodes within library element nodes Address all title attribute nodes within book elements anywhere in the document, which have the value “Artificial Intelligence” Address all books with title “Artificial Intelligence”


Download ppt "1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through."

Similar presentations


Ads by Google