Presentation is loading. Please wait.

Presentation is loading. Please wait.

XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML.

Similar presentations


Presentation on theme: "XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML."— Presentation transcript:

1 XP ATH - XML Path Language

2 W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML An XML document is a tree made up of nodes. XPath is a language for picking nodes and sets of nodes out of this tree using path expressions/location paths. An XPath expression returns either a node-set, a string, a Boolean, or a number.

3 XPath is a major component in the XSLT standard. XSL are XML-based Stylesheet Language. XPath became a W3C Recommendation 16. November 1999. XPath includes over 100 built-in functions. There are functions for String values, Numeric values, Boolean values, Date and Time comparison, Node manipulation and more. N O X SLT WITHOUT XP ATH !!

4 XP ATH N ODES XML documents are treated as trees of nodes. The topmost element of the tree is called the root element. In XPath, there are seven kinds of nodes: I. Root nodes II. Element nodes III. Attribute nodes IV. Text nodes V. Namespace nodes VI. Processing-instruction nodes VII. Comment nodes.

5 E XAMPLE : XP ATH N ODES Harry Potter J K. Rowling 2005 29.99 (root element node) J K. Rowling (element node) J K. Rowling (text node) lang="en" (attribute node) (processing-instruction node) (comment node) (namespace node) XML DocNodes

6 R ELATIONSHIP OF N ODES Nodes in an XML are related in the following way by XPath: Parent Each element/attribute has one parent. Children Element nodes may have 0, 1 or more children. Siblings Nodes that have the same parent. Ancestors A node's parent, parent's parent, etc. Descendants A node's children, children's children, etc.

7 Harry Potter J K. Rowling 2005 29.99 In the above example; book - parent of the title, author, year, and price. title, author, year and price - children of book element. title, author, year and price - siblings. book, bookstore element - ancestors of title element. book, title, author etc. elements - descendants of bookstore. E XAMPLE : XP ATH N ODES

8 XP ATH – L OCATION P ATHS Location Path are the Path Expressions. XPath uses Location Paths to select nodes or node-sets in an XML document. Expression / Location Path Description nodename Selects all nodes with the name "nodename" / 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

9 A location path can be absolute or relative. An absolute location path starts with a slash ( / ) /step/step/... and a relative location path does not. step/step/... The XPath expressions such as element name, @attribute name, /, comment( ), text( ), and processing-instruction( ) are all Single Location Paths/Steps. Location Paths can be combined with “ / ” to move around the hierarchy from the matched node to other nodes. Such Location Paths are called Compound Location Paths. Ex: /people/person/name/first_name. The above compound location XPath expression lists the first name of all the persons listed inside the people element. XP ATH – L OCATION P ATHS

10 XP ATH : S ELECTING N ODES Harry Potter 29.99 Learning XML 39.95 Path ExpressionResult bookstore Selects all nodes with the name "bookstore" /bookstore Selects the root element bookstore/book Selects all book elements that are children of bookstore //book Selects all book elements no matter where they are in the document bookstore//book Selects all book elements that are descendant of bookstore. //@lang Selects all attributes that are named lang

11 N ODE T ESTS – M ORE L OCATION P ATHS Having covered element, attribute and root nodes above, the rest 4 kinds of nodes i.e. namespace, text, processing-instruction and comment nodes can also be selected as below: Node TestDescription comment() Selects nodes that are comments. node() Selects nodes of any type. processing- instruction() Selects nodes that are processing instructions. You can specify which processing instruction to select by providing it's name in the parentheses. text() Selects a text node.

12 XP ATH : P REDICATES Predicates (embedded in square brackets) are used to find a specific node or a node that contains a specific value. Path ExpressionResult /bookstore/book[1] Selects first book element that is child of bookstore element /bookstore/book[last()] Selects last book element that is child of the bookstore /bookstore/book[last()-1] Selects last but one book element that is child of bookstore /bookstore/book [position()<3] Selects first 2 book elements that are children of bookstore //title[@lang] Selects all title elements that have an attribute named lang //title[@lang='en'] Selects all title elements having "lang" attribute with “en” value /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]/title Selects all the title elements of book elements of bookstore element having price with a value greater than 35.00

13 XP ATH : W ILDCARDS XPath wildcards can be used to select unknown XML elements. Example: WildcardDescription * Matches any element node @* Matches any attribute node node() Matches any node of any kind Path ExpressionResult /bookstore/* Selects all child element nodes of bookstore element //* Selects all elements in the document //title[@*] Selects all title elements having at least 1 attribute

14 C OMPLEX XP ATH E XAMPLES : WITH WILD-CARDS, NODE TESTS, PREDICATES Path ExpressionResult bookstore/*/title All elements that are grandchildren of elements. /bookstore/book[1]/title Select the title of the first book /bookstore/book/price[text()] Selects the text from all price nodes //Participant [string-length(FirstName)>=8] Return all Participant nodes with a contents of FirstName bigger than 7 characters: /bookstore/book[price>35]/title Select title nodes with price>35 book/@style The style attribute for all elements of current context. books//book[contains(title, 'XQuery')]/title/text() Get all the books that contain the word 'XQuery" somewhere in the title

15 XP ATH : S EVERAL P ATHS By using the | operator in an XPath expression you can select several paths. Example: Path ExpressionResult //book/title | //book/price Selects all the title AND price elements of all book elements //title | //price Selects all the title AND price elements in the document /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 XP ATH O PERATORS OperatorDescriptionExample | Computes two node-sets//book | //cd + Additionemployee/id[6+ 4] - Subtractionemployee/id[6 – 4] * Multiplication//age[6 * 4] div Division//age[8 div 4] = Equal//item[@val='low'] != Not equal//item[price!=9.80] < Less than//item[price<9.80] <= Less than or equal to//item[price <= 2] > Greater than//exercise[note>5]/title >= Greater than or equal to//item[price>=9.80] or //item[price=9.80 or price=9.70] and //item[price>9.00 and price<9.90] mod Modulus (division remainder)//item/title[id > 5 mod 2]

17 XP ATH R ETURN T YPES Each XPath function returns one of these four types: Boolean, Number, Node-set, String. Types Example Boolean most commonly used in predicates of location paths. Example: person[profession="physicist"], profession="physicist" is a Boolean. Strings XPath strings are ordered sequences of Unicode characters such as "Fred", " ", or "". You can use the = and != comparison operators to check whether two strings are the same and relational, = operators to compare strings. Number XPath provides the five basic arithmetic operators : +, -, *, div, mod which can be used on numbers. Example: //person[@birth_century<= 1900 mod 100]] Node-set Set of nodes as a result of path expression.

18 XP ATH F UNCTIONS ON N ODE -S ETS NameDescription name(nodeset) Returns the name of the current node or the first node in the specified node set. root(node) Returns root of the tree to which the current node or the specified belongs. This will usually be a document node. count((item) Returns the count of nodes. Example : count[//person] position() Returns the index position of the node that is currently being processed. Example: //book[position()<=3] Result: Selects the first three book elements last() Returns the number of items in the processed node list. Example: //book[last()] Result: Selects the last book element

19 XP ATH F UNCTIONS ON S TRING NameDescription string(arg) Returns the string value of the argument. The argument could be a number, boolean, or node-set. Example: string(314) Result: "314" compare(comp1,comp2) Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2. Example: compare('ghi', 'ghi') Result: 0 concat(string,string,...) Returns the concatenation of the strings. Example: concat('XPath ','is ','FUN!') Result: 'XPath is FUN!' string-length(string) Returns the length of the specified string. Example: string-length('Beatles') Result: 7

20 upper-case(string) Converts string argument to upper-case. Example: upper-case('The XML') Result: 'THE XML' lower-case(string) Converts the string argument to lower- case. Example: lower-case('The XML') Result: 'the xml' starts- with(string1,string2) Returns true if string1 starts with string2, otherwise it returns false. Example: starts-with('XML','X') Result: true ends- with(string1,string2) Returns true if string1 ends with string2, otherwise it returns false. Example: ends-with('XML','X') Result: false tokenize(string,pattern) Example: tokenize("XPath is fun", "\s+") Result: ("XPath", "is", "fun") XP ATH F UNCTIONS ON S TRING

21 substring- before(string1,string2) Returns the start of string1 before string2 occurs in it. Example: substring-before('12/10','/') Result: '12' substring- after(string1,string2) Returns the remainder of string1 after string2 occurs in it. Example: substring-after('12/10','/') Result: '10' matches(string,pattern) Returns true if the string argument matches the pattern, otherwise, it returns false. Example: matches("Merano", "ran") Result: true replace(string, pattern,replace) Returns a string created by replacing the given pattern with given argument. Example: replace("Bella Italia", "l", "*") Result: 'Be**a Ita*ia' XP ATH F UNCTIONS ON S TRING

22 Contains (string1,string2) Returns true if string1 contains string2, otherwise returns false. Example: contains('XML','XM') Result: true Substring (string,start,len) substring (string,start) Returns the substring from the start position to specified length. If length is omitted it returns the substring from the start position to the end. Example: substring('Beatles',1,4) Result: 'Beat' normalize- space(string) Removes leading and trailing spaces from the string, and replaces all internal sequences of white space with one and returns the result. Example: normalize-space(' The XML ') Result: 'The XML' XP ATH F UNCTIONS ON S TRING

23 XP ATH F UNCTIONS ON N UMERIC V ALUES NameDescription number(arg) Returns numeric value of the argument. Arg. could be Boolean, string, or node-set. Example: number('100') -> Result: 100 abs(num) Returns the absolute value of argument. Example: abs(-3.14) -> Result: 3.14 ceiling(num) Returns the smallest integer that is greater than the number argument. Example: ceiling(3.14) -> Result: 4 floor(num) Returns the largest integer that is not greater than the number argument Example: floor(3.14) -> Result: 3 round(num) Rounds the number to nearest integer. Example: round(3.14) -> Result: 3 sum(arg,arg,...) Returns the sum of the numeric value of each node in the specified node-set.

24 XP ATH F UNCTIONS ON B OOLEAN V ALUES NameDescription boolean(arg) Returns a boolean value for a number, string, or node-set. not(arg) Returns true if the boolean value is false, and false if the boolean value is true. Example: not(true()) Result: false true() Returns the boolean value true. Example: true() Result: true false() Returns the boolean value false. Example: false() Result: false

25 Thank you !


Download ppt "XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML."

Similar presentations


Ads by Google