XPATH XML Path Language WHAT IS XPATH XPath

  • Slides: 25
Download presentation
XPATH - XML Path Language

XPATH - XML Path Language

WHAT IS XPATH? XPath, the XML Path Language, is a query language for selecting

WHAT IS XPATH? XPath, the XML Path Language, is a query language for selecting nodes from an XML document. 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.

NO XSLT WITHOUT XPATH !! XPath is a major component in the XSLT standard.

NO XSLT WITHOUT XPATH !! XPath is a major component in the XSLT standard. XSL are XML-based Stylesheet Language. XPath became a W 3 C 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.

XPATH NODES XML documents are treated as trees of nodes. The topmost element of

XPATH NODES 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: Root nodes II. Element nodes III. Attribute nodes IV. Text nodes V. Namespace nodes VI. Processing-instruction nodes VII. Comment nodes. I.

EXAMPLE : XPATH NODES XML Doc <? xml version="1. 0" encoding="UTF-8"? > <? xml-stylesheet

EXAMPLE : XPATH NODES XML Doc <? xml version="1. 0" encoding="UTF-8"? > <? xml-stylesheet type=“text/xsl” href=“style. xsl”? > <bookstore xmlns="http: //www. xml. com/mystore"> <! - - Some Comment here - - > <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29. 99</price> </bookstore> Nodes <bookstore> (root element node) <author>J K. Rowling</author> (element node) J K. Rowling (text node) lang="en" (attribute node) <? xml-stylesheet> (processing-instruction node) <!- - comment- -> (comment node) <xmlns> (namespace node)

RELATIONSHIP OF NODES Nodes in an XML are related in the following way by

RELATIONSHIP OF NODES 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.

EXAMPLE : XPATH NODES <bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29. 99</price>

EXAMPLE : XPATH NODES <bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29. 99</price> </bookstore> 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.

XPATH – LOCATION PATHS Location Path are the Path Expressions. XPath uses Location Paths

XPATH – LOCATION PATHS Location Path are the Path Expressions. XPath uses Location Paths to select nodes or nodesets in an XML document. Expression / Location Path nodename / //. . . @ Description 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

XPATH – LOCATION PATHS A location path can be absolute or relative. An absolute

XPATH – LOCATION PATHS A location path can be absolute or relative. An absolute location path starts with a slash ( / ) /step/. . . and a relative location path does not. 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.

XPATH : SELECTING NODES <? xml version="1. 0" encoding="UTF-8"? > <bookstore> <book> <title lang="en">Harry

XPATH : SELECTING NODES <? xml version="1. 0" encoding="UTF-8"? > <bookstore> <book> <title lang="en">Harry Potter</title> <price>29. 99</price> </book> <title lang="en">Learning XML</title> <price>39. 95</price> </bookstore> Path Expression Result bookstore Selects all nodes with the name "bookstore" /bookstore Selects the root element bookstore/book //bookstore//book //@lang Selects all book elements that are children of bookstore Selects all book elements no matter where they are in the document Selects all book elements that are descendant of bookstore. Selects all attributes that are named lang

NODE TESTS – MORE LOCATION PATHS Having covered element, attribute and root nodes above,

NODE TESTS – MORE LOCATION PATHS 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 Test Description comment() Selects nodes that are comments. node() Selects nodes of any type. processinginstruction() 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.

XPATH : PREDICATES Predicates (embedded in square brackets) are used to find a specific

XPATH : PREDICATES Predicates (embedded in square brackets) are used to find a specific node or a node that contains a specific value. Path Expression /bookstore/book[1] /bookstore/book[last()] Result Selects first book element that is child of bookstore element 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 bookstore element having price with a value greater than 35. 00

XPATH : WILDCARDS XPath wildcards can be used to select unknown XML elements. Wildcard

XPATH : WILDCARDS XPath wildcards can be used to select unknown XML elements. Wildcard Description * Matches any element node @* Matches any attribute node() Matches any node of any kind Example: Path Expression /bookstore/* //title[@*] Result Selects all child element nodes of bookstore element Selects all elements in the document Selects all title elements having at least 1 attribute

COMPLEX XPATH EXAMPLES : WITH WILD-CARDS, NODE TESTS, PREDICATES Path Expression Result bookstore/*/title All

COMPLEX XPATH EXAMPLES : WITH WILD-CARDS, NODE TESTS, PREDICATES Path Expression Result bookstore/*/title All <title> elements that are grandchildren of <bookstore> 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(First. Name)>=8] Return all Participant nodes with a contents of First. Name bigger than 7 characters: /bookstore/book[price>35]/title Select title nodes with price>35 book/@style The style attribute for all <book> elements of current context. books//book[contains(title, 'XQuery')]/title/text() Get all the books that contain the word 'XQuery" somewhere in the title

XPATH : SEVERAL PATHS By using the | operator in an XPath expression you

XPATH : SEVERAL PATHS By using the | operator in an XPath expression you can select several paths. Example: Path Expression Result //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

XPATH OPERATORS Operator Description Example | Computes two node-sets //book | //cd + Addition

XPATH OPERATORS Operator Description Example | Computes two node-sets //book | //cd + Addition employee/id[6+ 4] - Subtraction employee/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 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]

XPATH RETURN TYPES Each XPath function returns one of these four types: Boolean, Number,

XPATH RETURN TYPES 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 <, >, <=, and >= 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.

XPATH FUNCTIONS ON NODE-SETS Name Description name(nodeset) Returns the name of the current node

XPATH FUNCTIONS ON NODE-SETS Name Description 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

XPATH FUNCTIONS ON STRING Name Description Returns the string value of the argument. The

XPATH FUNCTIONS ON STRING Name Description Returns the string value of the argument. The argument could be a number, boolean, or node-set. Example: string(314) Result: "314" compare(comp 1, comp 2) Returns -1 if comp 1 is less than comp 2, 0 if comp 1 is equal to comp 2, or 1 if comp 1 is greater than comp 2. Example: compare('ghi', 'ghi') Result: 0 concat(string, . . . ) Returns the concatenation of the strings. Example: concat('XPath ', 'is ', 'FUN!') Result: 'XPath is FUN!' string(arg) string-length(string) Returns the length of the specified string. Example: string-length('Beatles') Result: 7

XPATH FUNCTIONS ON STRING upper-case(string) Converts string argument to upper-case. Example: upper-case('The XML') Result:

XPATH FUNCTIONS ON STRING upper-case(string) Converts string argument to upper-case. Example: upper-case('The XML') Result: 'THE XML' Converts the string argument to lowercase. Example: lower-case('The XML') Result: 'the xml' starts. Returns true if string 1 starts with string 2, with(string 1, string 2) otherwise it returns false. Example: starts-with('XML', 'X') Result: true ends. Returns true if string 1 ends with string 2, with(string 1, string 2) otherwise it returns false. Example: ends-with('XML', 'X') Result: false tokenize(string, pattern) Example: tokenize("XPath is fun", "s+") Result: ("XPath", "is", "fun") lower-case(string)

XPATH FUNCTIONS ON STRING substring. Returns the start of string 1 before string 2

XPATH FUNCTIONS ON STRING substring. Returns the start of string 1 before string 2 before(string 1, string 2) occurs in it. Example: substring-before('12/10', '/') Result: '12' substring. Returns the remainder of string 1 after(string 1, string 2) string 2 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, Returns a string created by replacing the pattern, replace) given pattern with given argument. Example: replace("Bella Italia", "l", "*") Result: 'Be**a Ita*ia'

XPATH FUNCTIONS ON STRING Contains (string 1, string 2) Returns true if string 1

XPATH FUNCTIONS ON STRING Contains (string 1, string 2) Returns true if string 1 contains string 2, otherwise returns false. Example: contains('XML', 'XM') Result: true Substring Returns the substring from the start position to (string, start, len) specified length. If length is omitted it returns the substring from the start position to the end. substring (string, start) Example: substring('Beatles', 1, 4) Result: 'Beat' normalizespace(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'

XPATH FUNCTIONS ON NUMERIC VALUES Name Description number(arg) Returns numeric value of the argument.

XPATH FUNCTIONS ON NUMERIC VALUES Name Description 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, . . . ) Returns the sum of the numeric value of each node in the specified node-set.

XPATH FUNCTIONS ON BOOLEAN VALUES Name Description boolean(arg) Returns a boolean value for a

XPATH FUNCTIONS ON BOOLEAN VALUES Name Description 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

Thank you !

Thank you !