Download presentation
Presentation is loading. Please wait.
Published byChastity Page Modified over 9 years ago
1
IS432 Semi-Structured Data Lecture 4: XPath Dr. Gamal Al-Shorbagy
2
2 What is Xpath ? XPath: "A language for addressing parts of an XML document" Similar to a DOS or UNIX "file system path" but with powerful expressions XPath is to XML what the SQL "select" statement is to SQL –But, XPath is not a full programming language or a query language.
3
What is XPath ? XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard – –XQuery and XPointer are both built on XPath expressions. XPath XPointer XQuery
4
4 XPath Related Standards XSLT – XPath is used to tell XSLT how to match tags XLink – similar to HTML links but more powerful XPointer - a standard manner for identifying document fragments XQuery – a newer, more comprehensive standard that includes XPath 2.0 and allows more complex searches and data types include relational database searches.
5
5 Versions Version 1.0 –W3C Recommendation November, 16 1999 –http://www.w3.org/TR/xpath Version 2.0 –W3C Working Draft October, 29 2004 –http://www.w3.org/TR/xpath20/
6
6 Other Familiar Path Names DOS: –C:\Program Files\Altova\XMLSPY2004\Examples\Tutorial Web –http://www.google.com/search?hl=en&lr=lang_en&&q=XPath Unix –/usr/local/lib/mylib/myprogram.jar Similarities –Absolute path starts with "/" –Relative paths express do not start with "/"
7
What is XPath ? A syntax for defining parts of an XML document Uses path expressions to navigate in XML documents Contains a library of standard functions A major element in XSLT (W3C recommendation)
8
XPath Terminology Seven Nodes of XPath –Element –Attribute –Text –Namespace –Processing-instruction –Comment –Document nodes. Atomic Values XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.
9
XPath Terminology Harry Potter J K. Rowling 2005 29.99 Root Element Attribute Atomic Value Element
10
XPath Syntax XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
11
Xpath Axis Relationships of Xpath Nodes
12
Xpath Axis ancestor parent child descendant Proceeding-sibling following-sibling Self Attribute bookstore book titleauthorfirstnamelastnamepublisher address paper magazine title
13
XPath Syntax Harry Potter 29.99 Learning XML 39.95
14
XPath Syntax: Selecting Nodes Path ExpressionDescription nodename Selects all nodes with the name "nodename" / Selects the root node // Selects nodes in the document from the current node that match the selection no matter where they are. Selects the current node.. Selects the parent of the current node @ Selects attributes
15
XPath Syntax: Selecting Nodes Path ExpressionResult bookstoreSelects all nodes with the name "bookstore" /bookstoreSelects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/bookSelects all book elements that are children of bookstore //bookSelects all book elements no matter where they are in the document bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element //@langSelects all attributes that are named lang
16
XPath Syntax: Selecting Nodes Path ExpressionResult bookstore /bookstore bookstore/book //book bookstore//book //@lang Harry Potter 29.99 XML 4 Dummies 39.95 The Han River 149.95
17
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.
18
Xpath Syntax: Predicates Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element. /bookstore/book[last()]Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1]Selects the second last book element that is the child of the bookstore element /bookstore/book[position()<3]Selects the first two book elements that are children of the bookstore element //title[@lang]Selects all the title elements that have an attribute named lang //title[@lang='eng']Selects all the title elements that have an attribute named lang with a value of 'eng' /bookstore/book[price>35.00]Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 /bookstore/book[price>35.00]/titleSelects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
19
Xpath Syntax: Predicates Path ExpressionResult /bookstore/book[1] /bookstore/book[last()] /bookstore/book[last()-1] /bookstore/book[position()<3] //title[@lang] //title[@lang=‘kor'] /bookstore/book[price>35.00] /bookstore/book[price>35.00]/title Harry Potter 29.99 XML 4 Dummies 39.95 The Han River 149.95
20
Xpath Example Max Toula Select all pet elements //pet or alternatively /pets/pet or /pets/child::* Select the first pet /pets/pet[1] Select all pets of type dog //pet[@type ="dog"] Select all pets of white color //pet[@color="white"] Select the color of all dogs //pet[@type ="dog"]/@color Get the types of pets with the name Max /pets/pet[text()="Max"]/@type
21
Xpath Syntax: Wild Cards WildcardDescription *Matches any element node @*Matches any attribute node node()Matches any node of any kind Path ExpressionResult /bookstore/*Selects all the child nodes of the bookstore element //*Selects all elements in the document //title[@*]Selects all title elements which have any attribute
22
Xpath Syntax: Selecting Multiple Paths Path ExpressionResult //book/title | //book/priceAll the title and price elements of all book elements //title | //priceAll title and Price elements /bookstore/book/title | //price All books (in bookstore) and All price elements
23
Xpath Axis ancestor::author parent::author child::firstname, (child::*), child::node() descendant::author proceeding-sibling::author following-sibling::author attribute::title bookstore book titleauthorfirstnamelastnamepublisher address paper magazine title
24
Xpath Functions Node-Set Takes a node-set argument, returns a node-set, or returns/provides information about a particular node within a node-set. String Performs evaluations, formatting, and manipulation on string arguments. BooleanEvaluates the argument expressions to obtain a Boolean result. NumberEvaluates the argument expressions to obtain a numeric result.
25
XPath Functions: Nodes Set node-setcount(node-set) //emp3 //emp[1]1 ABC 5000 PQR 7000 XYZ 9000
26
XPath Functions: Nodes Set node-setlast() //emp[last()] XYZ 9000 ABC 5000 PQR 7000 XYZ 9000
27
XPath Functions: String String concat("abc", "d", "ef", "g") abcdefg boolean contains(“Mobily”, “bil”) true String normalize-space(" abc def ") “abc def” boolean starts-with(string, string) number string-length("abcd") 4 String substring("12345",2,3) “234” …
28
XPath Functions: Number ceiling(2.5) = 3 floor(3.5) = 3 number(arg) number('2048') = 2048 number('-2048') = -2048 number('text') = NaN number('109.54') = 109.54 round(2.6) = 3, round (2.4) = 2, round(2.5) = 3 number sum(node-set)
29
Example for XPath Queries Addison-Wesley Serge Abiteboul Rick Hull Victor Vianu Foundations of Databases 1995 Freeman Jeffrey D. Ullman Principles of Database and Knowledge Base Systems 1998 Addison-Wesley Serge Abiteboul Rick Hull Victor Vianu Foundations of Databases 1995 Freeman Jeffrey D. Ullman Principles of Database and Knowledge Base Systems 1998
30
Data Model for XPath bib book publisherauthor.. Addison-WesleySerge Abiteboul The root The root element Much like the Xquery data model Processing instruction Comment
31
The Root and the Root 1 2 bib is the “document element” The “root” is above bib /bib = returns the document element / = returns the root Why ? Because we may have comments before and after ; they become siblings of This is advanced xmlogy
32
XPath: Simple Expressions /bib/book/year Result: 1995 1998 /bib/paper/year Result: empty (there were no papers)
33
XPath: Restricted Kleene Closure //author Result: Serge Abiteboul Rick Hull Victor Vianu Jeffrey D. Ullman /bib//first-name Result: Rick
34
Xpath: Functions /bib/book/author/text() Result: Serge Abiteboul Jeffrey D. Ullman Rick Hull doesn’t appear because he has firstname, lastname Functions in XPath: –text() = matches the text value –node() = matches any node (= * or @* or text()) –name() = returns the name of the current tag
35
Xpath: Wildcard //author/* Result: Rick Hull * Matches any element
36
Xpath: Attribute Nodes /bib/book/@price Result: “55” @price means that price is has to be an attribute
37
Xpath: Qualifiers /bib/book/author[firstname] Result: Rick Hull
38
Xpath: More Qualifiers /bib/book[@price < “60”] /bib/book[author/@age < “25”] /bib/book[author/text()]
39
Xpath: Summary bibmatches a bib element *matches any element /matches the root element /bibmatches a bib element under root bib/papermatches a paper in bib bib//papermatches a paper in bib, at any depth //papermatches a paper at any depth paper|bookmatches a paper or a book @pricematches a price attribute bib/book/@pricematches price attribute in book, in bib bib/book/[@price<“55”]/author/lastname matches…
40
References http://www.w3schools.com/xpath/default.asp http://msdn.microsoft.com/en-us/library/ms256086.aspx http://www.xpathtester.com/test http://oreilly.com/catalog/xmlnut/chapter/ch09.html http://msdn.microsoft.com/en-us/library/ms256086.aspx
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.