+ All Categories
Home > Documents > XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for...

XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for...

Date post: 23-Dec-2015
Category:
Upload: daniel-pearson
View: 235 times
Download: 5 times
Share this document with a friend
Popular Tags:
25
XPATH - XML Path Language
Transcript
Page 1: 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.

XPATH- XML Path Language

Page 2: 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.

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.

Page 3: 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.

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.

NO XSLT WITHOUT XPATH !!

Page 4: 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.

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:

I. Root nodes

II. Element nodes

III. Attribute nodes

IV. Text nodes

V. Namespace nodes

VI. Processing-instruction nodes

VII. Comment nodes.

Page 5: 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.

EXAMPLE : XPATH NODES

<?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>  </book></bookstore>

<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)

XML Doc Nodes

Page 6: 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.

RELATIONSHIP OF NODES

Nodes in an XML are related in the following way by XPath:

Parent Each element/attribute has one parent.

ChildrenElement nodes may have 0, 1 or more children.

SiblingsNodes that have the same parent.

Ancestors A node's parent, parent's parent, etc.

DescendantsA node's children, children's children, etc.

Page 7: 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.

<bookstore><book>  <title>Harry Potter</title>  <author>J K. Rowling</author>  <year>2005</year>  <price>29.99</price></book></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.

EXAMPLE : XPATH NODES

Page 8: 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.

XPATH – LOCATION PATHS

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

Page 9: 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.

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.

XPATH – LOCATION PATHS

Page 10: 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.

XPATH : SELECTING NODES<?xml version="1.0" encoding="UTF-8"?>

<bookstore><book>  <title lang="en">Harry Potter</title>  <price>29.99</price></book>

<book>  <title lang="en">Learning XML</title>  <price>39.95</price></book></bookstore>

Path Expression Result

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

Page 11: 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.

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.

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.

Page 12: 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.

XPATH : PREDICATESPredicates (embedded in square brackets) are used to find a specific node or a node that contains a specific value.

Path Expression Result

/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

Page 13: 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.

XPATH : WILDCARDS

XPath wildcards can be used to select unknown XML elements.

Example:

Wildcard Description

* Matches any element node

@* Matches any attribute node

node() Matches any node of any kind

Path Expression Result

/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

Page 14: 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.

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(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 <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

Page 15: 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.

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

Page 16: 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.

XPATH OPERATORSOperator 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 and //item[price>9.00 and price<9.90]

mod Modulus (division remainder) //item/title[id > 5 mod 2]

Page 17: 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.

XPATH RETURN TYPESEach 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.

Page 18: 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.

XPATH FUNCTIONS ON NODE-SETSName 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

Page 19: 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.

XPATH FUNCTIONS ON STRINGName Description

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

Page 20: 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.

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")

XPATH FUNCTIONS ON STRING

Page 21: 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.

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'

XPATH FUNCTIONS ON STRING

Page 22: 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.

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'

XPATH FUNCTIONS ON STRING

Page 23: 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.

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 argumentExample: 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.

Page 24: 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.

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

Page 25: 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.

Thank you !


Recommended