1 xpath modified slides from dr. sagiv. 2 xpath a language for locating nodes in xml documents xpath...

27
1 XPATH Modified Slides from Dr. Sagiv

Upload: amberlynn-henry

Post on 22-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

1

XPATHXPATH

Modified Slides from

Dr. Sagiv

2

XPath• A Language for Locating Nodes in XML Documents• XPath expressions are written in a syntax that

resembles paths in file systems• The list of nodes located by an XPath expression is

called a Nodelist• XPath is used in XSL and in XQuery (a query language

for XML)• W3Schools has an XPath tutorial• XPath includes

– Axis navigation– Conditions– Functions

3

<?xml version="1.0" encoding="ISO-8859-1"?><catalog> <cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cd country="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog>

<?xml version="1.0" encoding="ISO-8859-1"?><catalog> <cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cd country="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog>

An XML document

4

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

USA

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

5

The Main Idea in the Syntax of XPath Expressoins

• “/” at the beginning of an XPath expression represents the root of the document

• “/” between element names represents a parent-child relationship

• “//” represents an ancestor-descendent relationship

• “@” marks an attribute• “[condition]” specifies a condition

6

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog

Getting the root element of the document

USA

7

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog/cd

Finding child nodes

USA

8

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog/cd/price

Finding descendent nodes

USA

9

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog/cd[price<10]

Condition on elements

USA

10

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

//title

// represents any directed path in the document

/catalog//title

USA

11

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog/cd/*

* represents any element name in the document

USA

12

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

What will the followingexpressions return?

USA

* represents any element name in the document

13

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog/cd[1]

Position based condition

/catalog/cd[last()]

USA

14

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

/catalog/cd[@country=“UK”]

@ marks attributes

USA

15

catalog.xml

catalog

cd cd cdcountry

title artist price title artist price title artist price

countrycountry

UK UK

Dark Side of the Moon

Space Oddity Aretha: Lady Soul

Pink Floyd David Bowie Aretha Franklin

10.90 9.90 9.90

@ marks attributes

USA

/catalog/cd/@country

16

Relative Navigation Using Axes

• Starts with the current node and not with the root (/)

• A . marks the current node (e.g., ./title)

• A .. marks the parent node (e.g., title/../*)

• There are also other axes, e.g., child, descendent, ancestor, parent, following-sibling, etc.

17

Functions

• Many functions that are included in XPath

• Some examples:– count() – returns the number of nodes in a

nodelist– last() – returns the last node in a nodelist– name() – returns the name of a node– position() – returns the position of the node

in the nodelist

18

Additional Examples ofXPath Expressions

These examples use element names that are not necessarily from the XML document that was shown previously

19

Examples of XPath Expressions

• para– Selects the para children elements of the context

node

• *– Selects all element children of the context node

• text()– Selects all text node children of the context node

• @name– Selects the name attribute of the context node

20

More Examples ofXPath Expressions

• @*– Selects all the attributes of the context node

• para[1]– Selects the first para child of the context node

• para[last()]– Selects the last para child of the context node

• */para– Selects all para grandchilren of the context node

21

More Examples ofXPath Expressions

• /doc/chapter[5]/section[2]– Selects the second section of the fifth chapter of the

doc

• chapter//para– Selects the para element descendants of the chapter

element children of the context node

• //para– Selects all the para descendants of the document

root and thus selects all para elements in the same document as the context node

22

More Examples ofXPath Expressions

• //olist/item– Selects all the item elements that have an olist

parent and are in the same document as the context node

• .– Selects the context node

• .//para– Selects the para descendants of the context node

• ..– Selects the parent of the context node

23

More Examples ofXPath Expressions

• ../@lang– Selects the lang attribute of the parent of the

context node

• para[@type=“warning”]– Selects the para children of the context node

that have a type attribute with value warning

• chapter[title]– Selects the chapter children of the context

node that have one or more title children

24

More Examples ofXPath Expressions

• para[@type=“warning”][5]– Selects the fifth para child among the

children of the context node that have a type attribute with value warning

• para[5][@type=“warning”]– Selects the fifth para child of the context

node if that child has a type attribute with value warning

25

More Examples ofXPath Expressions

• chapter[title=“Introduction”]– Selects the chapter children of the context

node that have one or more title children with string-value equal to Introduction

• employee[@secretary and @assistant]– Selects employee children of the context

node that have both a secretary attribute and an assistant attribute

26

More Examples of Xpath Expressions

• /university/department/course– This Xpath expression matches any path that

starts at the root, which is a university element, passes through a department element and ends in a course element

• ./department/course[@year=2002]– This Xpath expression matches any path that

starts at the current element, continues to a child which is a department element and ends at a course element with a year attribute that is equal to 2002

27

Location Paths

• The previous examples are abbreviations of location paths– See XPath tutorial in W3Schools

– For example, // is short for /descendant-or-self::node()/.

• //para is short for /descendant-or-self::node()/child::para