xpath

36
XPath Kanda Runapongsa ([email protected] ) Dept. of Computer Engineering Khon Kaen University

Upload: heman

Post on 06-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

XPath. Kanda Runapongsa ( [email protected] ) Dept. of Computer Engineering Khon Kaen University. What is XPath?. XPath is a language designed to address specific parts of an XML document It was designed to be used by both XSLT and XPointer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: XPath

XPath

Kanda Runapongsa ([email protected])

Dept. of Computer EngineeringKhon Kaen University

Page 2: XPath

168493: XML and Web Services (II/2546)

2

What is XPath? XPath is a language designed to

address specific parts of an XML document

It was designed to be used by both XSLT and XPointer XSLT: transforms an XML document into

any text-based format, such as HTML XPointer: the basis for a fragment

identifies only for text/xml & application/xml media types

Page 3: XPath

168493: XML and Web Services (II/2546)

3

XPath Expressions The XPath data model views a

document as a tree of nodes An instance of the XPath

language is called an expression A path expression is an

expression used for selecting a node set by following a path or steps

Page 4: XPath

168493: XML and Web Services (II/2546)

4

XPath Expressions (Cont.)

XPath use path expressions to identify nodes in an XML document

These path expressions look very much like the expressions you see when you work with a computer file systemDocuments/courses/168493/xpath.ppt

Page 5: XPath

168493: XML and Web Services (II/2546)

5

Sample Document<?xml version=“1.0”?><catalog>

<cd country=“UK”><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>

</cd><cd country=“USA”>

<title>Greatest Hits</title><artist>Dolly Parton</artist><price>10.90</price>

</cd></catalog>

Page 6: XPath

168493: XML and Web Services (II/2546)

6

Path Expressions Examples

/catalogThis XPath expression selects the

ROOT element catalog /catalog/cdThis XPath expression selects all the

cd elements of the catalog element /catalog/cd/priceThis XPath expression selects all the

price elements of all the cd elements of the catalog element

Page 7: XPath

168493: XML and Web Services (II/2546)

7

/ and // / select the root node of the current

documentExample: /catalogSelect the root element catalog An absolute path to an element

// select nodes that are the descendant nodes of context nodeExample: //cdSelect the cd elements in the document

Page 8: XPath

168493: XML and Web Services (II/2546)

8

Selecting Unknown Elements

Wildcards (*) can be used to select unknown elements

/catalog/cd/*This XPath expression selects all

the child elements of all the cd elements of the catalog element

Page 9: XPath

168493: XML and Web Services (II/2546)

9

Selecting Unknown Elements (Cont.)

/catalog/*/priceThis expression selects all the price

elements that are grandchild elements of the catalog element

/*/*/price This expression selects all price

elements which have 2 ancestors How to select all elements?

Page 10: XPath

168493: XML and Web Services (II/2546)

10

Selecting Branches By using square brackets in an

XPath expression you can specify an element further

/catalog/cd[1]The XPath expression selects the

first cd child element of the catalog element

Page 11: XPath

168493: XML and Web Services (II/2546)

11

Selecting Branches (Cont.)

/catalog/cd[last()] The expression selects the last cd

element of the catalog element /catalog/cd[price]The expression selects all the cd

elements of the catalog element that have a price element

Page 12: XPath

168493: XML and Web Services (II/2546)

12

Selecting Branches (Cont.)

/catalog/cd[price=10.90]Selects all the cd elements of the

catalog element that have a price element with a value of 10.90

/catalog/cd[price=10.90]/priceSelects all the price elements of all the

cd elements of the catalog element that have a price element with a value of 10.90

Page 13: XPath

168493: XML and Web Services (II/2546)

13

Selecting Several Branches

By using the | operator in an XPath expression you can select several paths

/catalog/cd/title | /catalog/cd/artist

Selects all the title and artist elements of the cd element of the catalog element

Page 14: XPath

168493: XML and Web Services (II/2546)

14

Selecting Attributes In XPath all attributes are

specified by the @ prefix //@countrySelects all attributes named

country //cd[@country]Selects all cd elements which have

an attribute named country

Page 15: XPath

168493: XML and Web Services (II/2546)

15

Selecting Attributes (Cont.)

//cd[@*]Selects all cd elements which

have any attribute //cd[@country=‘UK’]Selects all cd elements which

have an attribute named country with a value of ‘UK’

Page 16: XPath

168493: XML and Web Services (II/2546)

16

A Location Path It is the most important kind of

expressions in the XPath notation It can be absolute or relative An absolute path starts with a slash

(/) and a relative location path does not start with a slash

The location path consists of one or more location steps, each separated by a slash

Page 17: XPath

168493: XML and Web Services (II/2546)

17

A Location Path (Cont.) An absolute location path:/step/step/… A relative location path:step/step/… The location steps are evaluated in

order one at a time, from left to right

Each step is evaluated against the nodes in the current node-set

Page 18: XPath

168493: XML and Web Services (II/2546)

18

A location Path (Cont.) The current node-set that is being

selected is called the set of the context nodes

Each step of a location path (a location step) has three following parts: An axis – specifies the tree relationship

between the nodes selected by the location step and the current node

A node test – specifies the node type Zero or more predicates – use

expressions to further refine the set of nodes

Page 19: XPath

168493: XML and Web Services (II/2546)

19

A Location Step The syntax for a location step is:axisname::nodetest[predicate] Example:child::cd[price=9.90] An axis defines a node-set

relative to the current node A node test is used to identify a

node with an axis

Page 20: XPath

168493: XML and Web Services (II/2546)

20

Axis Names ancestor: contains all ancestors

(parent, grandparent, etc.) of the current node

ancestor-or-self: contains the current node plus all its ancestors (parent, grandparent, etc.) What is the node that this axis

always includes?

Page 21: XPath

168493: XML and Web Services (II/2546)

21

Axis Names (Cont.) attribute: contains all attributes of

the current node child: contains all children of the

current node descendant: contains all

descendants (children, grandchildren, etc.) of the current node Does this axis contain attribute nodes?

Page 22: XPath

168493: XML and Web Services (II/2546)

22

Axis Names (Cont.) descendant-or-self: contains the

current node plus all its descendants

following: contains everything in the document after the closing tag of the current node, except descendants

following-sibling: contains all siblings after the current node

Page 23: XPath

168493: XML and Web Services (II/2546)

23

Axis Names (Cont.) namespace: contains all

namespace nodes of the current node

parent: contains the parent of the current node

preceding: contains everything in the document that is before the starting tag of the current node, except ancestors

Page 24: XPath

168493: XML and Web Services (II/2546)

24

Axis Names (Cont.) preceding-sibling: contains all

siblings before the current node self: contains the current node For any given context node v, the

four major axes specify a partitioning of the document containing vv/descendants U v/ancestors U

v/following U v/preceding U {v}

Page 25: XPath

168493: XML and Web Services (II/2546)

25

Examples child:cdSelects all cd elements that are

children of the current node attribute::srcSelects the src attribute of the

current node

Page 26: XPath

168493: XML and Web Services (II/2546)

26

Examples (Cont.) child::cd[position() < 6]Selects the first five cd children

of the current node child::cd[attribute::type=“class

ic”]Selects all cd children of the

current node that have a type attribute with value classic

Page 27: XPath

168493: XML and Web Services (II/2546)

27

Unabbreviated vs. Abbreviated

The name of the location path is long Location paths can be expressed

using abbreviated syntax The most important abbreviation is

child:: which can be omitted from a location step

Example:cd is short for child::cd

Page 28: XPath

168493: XML and Web Services (II/2546)

28

Abbreviated Syntax @ is short for attributes::Example:

cd[@type=“classic”] is short for child::cd[attribute::type=“classic”]

. is short for self::node()Example::

./cd is short for self::node()/child::cd

Page 29: XPath

168493: XML and Web Services (II/2546)

29

Abbreviated Syntax (Cont.)

.. is short for parent::nodeExample:../cd is short forparent::node()/child::cd // is short for /descendant-or-

self::node()/Example:://cd is short for/descendant-or-self::node()/child::cd

Page 30: XPath

168493: XML and Web Services (II/2546)

30

Examples cd[@type=“classic”]Selects all cd children of the

current node that has a type attribute with value classic

cd[@type=“classic”][5]Selects the fifth cd child of the

current node that has a type attribute with value classic

Page 31: XPath

168493: XML and Web Services (II/2546)

31

Examples cd[5][@type=“classic”]Selects the fifth cd child of the

current node if that child has a type attribute with value classic

cd[@type and @country]Selects all the cd children of the

current node that have both a type attribute and a country attribute

Page 32: XPath

168493: XML and Web Services (II/2546)

32

Node Set Functions Function count() counts the

number of selected elements Function name() returns name of

the element Function position() returns the

position in the node list of the node that is currently being processed

Page 33: XPath

168493: XML and Web Services (II/2546)

33

String Functions Function contains() returns true if

the 1st argument string contains the 2nd argument stringExample: contains(‘XML’, ‘M’)Result: true

Function string-length() returns the number of characters in the stringExample: string-length(‘XML’)Result: 3

Page 34: XPath

168493: XML and Web Services (II/2546)

34

String Functions (Cont.) Function substring() returns the

substring of the first argument starting at the position specified in the 2nd argument with length specified in the 3rd argumentExample: substring(‘Students’,4,4)Result: ‘dent’

Page 35: XPath

168493: XML and Web Services (II/2546)

35

String Functions (Cont.) Function starts-with() returns true if

the first argument starts with the second argumentExample: starts-with(‘XML’, ‘X’)Result: true

Function string() converts the value argument to a stringExample: string(314)Result: ‘314’

Page 36: XPath

168493: XML and Web Services (II/2546)

36

Number Functions celing() returns the smallest

integer that is not less than the number argumentExample: ceiling(3.14) 4

sum() returns the total value of a set of numeric values in a node-setExample: sum(/cd/price)