Tutorial –XPath, XQuery

Tutorial ? XPath, XQuery

CSCC43 - Introduction to Databases

CSCC43: Intro. to Databases

1

XPath Terminology

? Node

? document root, element, attribute, text, comment, ...

? Relationship

? parent, child, sibling, ancestor, descendent, ...

? Exercise: Identify nodes and relationships in following xml document

Harry Potter

79.99

Learning XML 69.95

document root does not correspond to anything

Intro. to Databases

in the document

39.00

CSCC43: Intro. to Databases

2

Node selector

Expression / node //

. .. @

Description Selects the document root node (absolute path) Selects the node (relative path) Selects all descendent nodes of the current node that match the selection Selects the current node Selects the parent of the current node Selects attribute nodes

CSCC43: Intro. to Databases

3

Node selector: exercise

Result Selects the document root node Selects the bookstore element node Selects all book element nodes Selects all price element nodes Selects all lang attribute nodes

Path Expression

? ?

? ?

? ?

? ?

?

?

././.

?

/bookstore//@lang/../..

?

./book/tilte/@lang

CSCC43: Intro. to Databases

4

Node selector : exercise sol

Result

Path Expression

Selects the document root node

/

/.

Selects the bookstore element node

/bookstore

./bookstore

Selects all book element nodes

/bookstore/book

//book

Selects all price element nodes

bookstore/book/price

//price

Selects all lang attribute nodes

//@lang

Selects the document root node

././.

Selects all the book element nodes

/bookstore//@lang/../..

Selects the empty set

./book/tilte/@lang

CSCC43: Intro. to Databases

5

Node selector: more exercise

Result Selects text nodes of all price element nodes Select all child nodes of book element nodes Select all comment nodes

Select all nodes except attribute nodes

Select all attribute nodes

?

?

Path Expression ?

?

? ? ? /bookstore/book/text() /bookstore/book/title/..//@*

CSCC43: Intro. to Databases

6

Node selector: more exercise sol

Result Selects text nodes of all price element nodes Select all child nodes of book element nodes Select all comment nodes

Select all nodes except attribute nodes

Select all attribute nodes

Selects empty set

Select all attribute nodes which are descendant of book element nodes

Path Expression //price/text()

/bookstore/book/*

//comment() //node() //@* /bookstore/book/text() /bookstore/book/title/..//@*

CSCC43: Intro. to Databases

7

XPath Syntax and Semantics

? Syntax

? locationStep1/locationStep2/...

? locationStep = axis::nodeSelector[predicate]

? Semantics

? Find all nodes specified by locationStep1

? Find all nodes specified by axis::nodeSelector ? Select only those that satisfy predicate

? For each such node N:

? Find all nodes specified by locationStep2 using N as the current node

? Take union

? For each node returned by locationStep2 do the same using locationStep3, ...

CSCC43: Intro. to Databases

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download