Presentation is loading. Please wait.

Presentation is loading. Please wait.

XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates.

Similar presentations


Presentation on theme: "XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates."— Presentation transcript:

1 XPath Presented by Kushan Athukorala

2 2 Agenda XPath XPath Terminology Selecting Nodes Predicates

3 3 XPath XPath is used to navigate through elements and attributes in an XML document XPath is a language for finding information in an XML document 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 a W3C recommendation

4 4 XPath Terminology Nodes In XPath, there are seven kinds of nodes 1.Element node 2.Attribute node 3.Text node 4.Namespace node 5.Processing-instruction node 6.Comment node 7.Document node Example – Consider the following XML Harry Potter J K. Rowling 2005 29.99 (root element node) J K. Rowling (element node) lang="en" (attribute node)

5 5 XPath Terminology contd. Atomic values Atomic values are nodes with no children or parent Examples J K. Rowling "en" Items Items are atomic values

6 6 Relationship of Nodes Example Harry Potter J K. Rowling 2005 29.99 Parent book element is the parent of the title, author, year, and price Children title, author, year, and price elements are all children of the book element Siblings Nodes that have the same parent title, author, year, and price elements are all siblings Ancestors ancestors of the title element are the book element and the bookstore element Descendants descendants of the bookstore element are the book, title, author, year, and price elements

7 7 Selecting Nodes XPath uses path expressions to select nodes in an XML document ExpressionDescription nodenameSelects all child nodes of the named node /Selects from 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

8 8 Selecting Nodes - Examples Path ExpressionResult bookstoreSelects all the child nodes of the bookstore element /bookstoreSelects the root element bookstoreNote: 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

9 9 Predicates Predicates are used to find a specific node or a node that contains a specific value Predicates are always embedded in square brackets Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element.Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!! /bookstore/book[last()]Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1]Selects the last but one 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

10 10 XPath Wildcards 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

11 11 XPath Axes An axis defines a node-set relative to the current node AxisNameResult ancestorSelects all ancestors (parent, grandparent, etc.) of the current node ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself attributeSelects all attributes of the current node childSelects all children of the current node descendantSelects all descendants (children, grandchildren, etc.) of the current node descendant-or-selfSelects all descendants (children, grandchildren, etc.) of the current node and the current node itself followingSelects everything in the document after the closing tag of the current node following-siblingSelects all siblings after the current node namespaceSelects all namespace nodes of the current node parentSelects the parent of the current node precedingSelects everything in the document that is before the start tag of the current node preceding-siblingSelects all siblings before the current node selfSelects the current node

12 12 XPath Axes - Examples ExampleResult child::bookSelects all book nodes that are children of the current node attribute::langSelects the lang attribute of the current node child::*Selects all children of the current node attribute::*Selects all attributes of the current node child::text()Selects all text child nodes of the current node child::node()Selects all child nodes of the current node descendant::bookSelects all book descendants of the current node ancestor::bookSelects all book ancestors of the current node ancestor-or-self::bookSelects all book ancestors of the current node - and the current as well if it is a book node child::*/child::priceSelects all price grandchildren of the current node

13 13 XPath Operators OperatorDescriptionExampleReturn value |Computes two node-sets//book | //cdReturns a node-set with all book and cd elements +Addition6 + 410 -Subtraction6 - 42 *Multiplication6 * 424 divDivision8 div 42 =Equalprice=9.80true if price is 9.80 false if price is 9.90 !=Not equalprice!=9.80true if price is 9.90 false if price is 9.80 <Less thanprice<9.80true if price is 9.00 false if price is 9.80 <=Less than or equal toprice<=9.80true if price is 9.00 false if price is 9.90 >Greater thanprice>9.80true if price is 9.90 false if price is 9.80 >=Greater than or equal toprice>=9.80true if price is 9.90 false if price is 9.70 or price=9.80 or price=9.70true if price is 9.80 false if price is 9.50 and price>9.00 and price<9.90 true if price is 9.80 false if price is 8.50 modModulus (division remainder)5 mod 21

14 14 XPath Functions Functions Reference Accessor Error and Trace Numeric String AnyURI Boolean Duration/Date/Time QName Node Sequence Context

15 15 Thank You

16 16 USA INDIA SRILANKA UK www.virtusa.com © V I r t u s a C o r p o r a t i o n "Virtusa" is a trademark of the company and a registered trademark in the EU and In India. "Productization" is a service mark of the company and a registered service mark in the United States. "vRule" is a service mark of the company. For more information please contact SalesInquiries@virtusa.com


Download ppt "XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates."

Similar presentations


Ads by Google