+ All Categories
Home > Documents > XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for...

XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for...

Date post: 26-Jul-2020
Category:
Upload: others
View: 14 times
Download: 0 times
Share this document with a friend
73
XML, XPath Lecture 4: MI-PDB, MIE-PDB: Advanced Database Systems Lecturer: Martin Svoboda [email protected] Author: Irena Holubová Faculty of Mathematics and Physics, Charles University in Prague Course NPRG036: XML Technologies 15. 3. 2016 http://www.ksi.mff.cuni.cz/~svoboda/courses/2015-2-MIE-PDB/
Transcript
Page 1: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XML, XPath Lecture 4:

MI-PDB, MIE-PDB: Advanced Database Systems

Lecturer: Martin Svoboda [email protected]

Author: Irena Holubová Faculty of Mathematics and Physics, Charles University in Prague Course NPRG036: XML Technologies

15. 3. 2016

http://www.ksi.mff.cuni.cz/~svoboda/courses/2015-2-MIE-PDB/

Page 2: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Outline

XML format and data model

Overview of XML technologies

XPath

Page 3: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XML

XML (eXtensible Markup Language) is a format for transfer and exchange of general data

Extensible Markup Language (XML) 1.0 (Fifth Edition)

http://www.w3.org/TR/xml/

Extensible Markup Language (XML) 1.1 (Second Edition)

http://www.w3.org/TR/xml11/

XML is a subset (application) of SGML (Standard Generalized Markup Language - ISO 8879) – from 1986

XML does not deal with data presentation

It enables to tag parts of the data

The meaning of the tags depends on the author

Presentation is one possible example

Page 4: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Document Structure

<?xml version="1.1" encoding="iso-8859-2"?>

<message>

<!–- content -->

<address>

<name>Tim Berners-Lee</name>

<street>Northern 12</street>

</address>

<intro>Hi!</intro>

<text>My <it>Internet</it> does not work!</text>

<signature>Steve J.</signature>

<attachment fig="image01.jpg"/>

</message>

Element with text

content

Attribute

Element with

element

content

Root element

Element with mixed

content

Prolog

Comment

Empty element

Page 5: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XML Document

XML document is well-formed, if:

It has introductory prolog

Start and end tags nest properly Each element has a start and an end tag

Corresponding tags have the same name (case

sensitivity)

<a></A>

Pairs of tags do not cross

<a><b></a></b>

The whole document is enclosed in a single root element

Page 6: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XML Infoset

A well formed XML document → hierarchical tree structure = XML Infoset

Abstract data model of XML data

Information set = the set of information (in the XML document)

Information item = a node of the XML tree

Types of items: document, element, attribute, string, processing instruction, comment, notation, DTD declaration, …

Properties of items: name, parent, children, content, …

It is used in other XML technologies

DTD (in general XML schema) can „modify“ Infoset

E.g. default attribute values

Page 7: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XML Technologies

XML

data

relational

XML DB native

XML DB

parsing validation

SAX

DOM

DTD

XML Schema

RELAX NG

Schematron StAX

LINQ querying

XPath XQuery

transformation

XSLT

XHTML

XQuery Update

MathML

SVG

XForms

DocBook

OpenDocument

OpenTravel

UBL

SOAP

UDDI

persistence

exploitation

SQL/XML

Page 8: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Query Languages for XML Data

Aims: querying, views, transformations, actualization, … Since 1998 XML-QL, XQL, …

The development stabilized in W3C in languages XSLT, XPath, XQuery

XSLT is a language for data transformation Exploits XPath for targeting parts of XML document

Has XML syntax

XQuery is more suitable for querying – user-oriented Exploits XPath for targeting parts of XML document

Today: XPath 1.0 Note: XPath 2.0 XQuery

Page 9: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

What is XPath?

Basic language for querying XML data

Selecting parts of XML documents

The idea resembles navigation in a file system

XPath does not have XML syntax

XPath is exploited in multiple other XML

technologies

XSLT, XQuery, XML Schema, XPointer, …

Page 10: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Data Model

order

document

date

"10/10/2008" status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

Page 11: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Data Model

Types of nodes in the model Root node

Element node

Text node

Attribute node

Comment

Processing instruction

Namespace declaration

Root node does not represent root element but the whole document

XML Infoset?

Page 12: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Expression

XPath expression is a path

Path consists of steps

Absolute path: /Step1/Step2/…/StepN

Relative path: Step1/Step2/…/StepN

Page 13: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/

"10/10/2008"

XPath Expressions – Examples

Page 14: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order

"10/10/2008"

XPath Expressions – Examples

Page 15: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order/items/item

"10/10/2008"

XPath Expressions – Examples

Page 16: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order/items/item/@code

"10/10/2008"

XPath Expressions – Examples

Page 17: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order/items/item/price/text()

"10/10/2008"

XPath Expressions – Examples

Page 18: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order/items/item/color

"10/10/2008"

XPath Expressions – Examples

Page 19: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order/customer/name

"10/10/2008"

XPath Expressions – Examples

Page 20: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount price

text() text()

"1" "91934"

color

text()

"blue"

/order/item-list/item

"10/10/2008"

XPath Expressions – Examples

Page 21: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Expressions – Examples

A relative path itself cannot be evaluated separately

It does not make sense, because we do not know where to start

The input must involve both the relative path and one or more nodes of XML documents where the evaluation should start

so-called context set

price/text()

Page 22: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

price/text()

XPath Expressions – Examples

Page 23: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

price/text()

XPath Expressions – Examples

Page 24: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

price/text()

XPath Expressions – Examples

Page 25: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

price/text()

XPath Expressions – Examples

Page 26: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Evaluation of XPath Expression

Let P be an XPath path

Let C be the context set of nodes for evaluation of P

If P is absolute, then C contains the root node of the document

If P is relative, then C must be specified explicitly

If P is empty, then the result of evaluation corresponds to C

Else, P is evaluated with regards to C as follows:

Let S be its first step and P’ is the rest of the path, i.e. P = S/P’

C’ = {}

For each node u from C evaluate S and add the result to C’

Evaluate P’ with regard to C’

Page 27: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Paths Formally

XPath step is formally the following expression:

Axis, node test and list of predicates

Predicates are optional

So far we saw only node tests

The list of predicates was empty

Axes were abbreviated

axis::node-test predicate1 ... predicateN

???

Page 28: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axes

Axis specifies the relation of nodes selected in this step with regard to each node u from context set C

Selected nodes are child nodes of node u

Most common axis

axis::node-test predicate1 ... predicateN

child

/order/customer

/child::order/child::customer abbreviation

Page 29: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis child

All child nodes of node u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 30: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis self

The selected node is u itself

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 31: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis parent

Parent node of node u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 32: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis ancestor

All ancestor nodes of node u

All nodes on the path from u to root node

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 33: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis ancestor-or-self

All ancestor nodes of node u including u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 34: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis descendant

All descendant nodes of node u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 35: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis descendant

All descendant nodes of node u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 36: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis descendant-or-self

All descendant nodes of node u including u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 37: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Tree Traversal

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1"

0

1

2

4

5

6

7

8

9

10

11

12

13

14 3

"blue"

Page 38: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis preceding-sibling

All siblings of u which precede it in tree traversal

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1"

0

1

2

4

5

6

7

8

9

10

11

12

13

14 3

"blue"

Page 39: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis preceding

All nodes which precede u in tree traversal Except for ancestors

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1"

0

1

2

4

5

6

7

8

9

10

11

12

13

14 3

"blue"

Page 40: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis following-sibling

All siblings of u which follow it in tree traversal

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1"

0

1

2

4

5

6

7

8

9

10

11

12

13

14 3

"blue"

Page 41: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis following

All nodes which follow u in tree traversal Except for ancestors

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

code

"929118813"

amount color

text() text()

"1"

0

1

2

4

5

6

7

8

9

10

11

12

13

14 3

"blue"

item

Page 42: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

All XPath Axes

ancestor

descendant

following

preceding

following-sibling preceding-sibling

child

attribute

namespace

self

parent

Page 43: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axis attribute

All attributes of node u

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

Page 44: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

Tests nodes selected by the axis

Node type, node name

axis::node-test predicate1 ... predicateN

Page 45: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

All nodes selected by the axis

axis::node() predicate1 ... predicateN

Page 46: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

descendant::node()

Page 47: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

All text nodes selected by the axis

axis::text() predicate1 ... predicateN

Page 48: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

descendant::text()

Page 49: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

All nodes selected by the axis which have a name

Name can have an element or an attribute

Note: there exists no axis that enables to selected

elements and attributes at the same time

axis::* predicate1 ... predicateN

axis::name predicate1 ... predicateN

All nodes with the specified name

Page 50: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Node Test

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

descendant::*

Page 51: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Axes and Node Test

Abbreviations

For the most commonly used axes and node

tests

//customer selects all elements customer

in XML document

…/… <=> …/child::…

…/@… <=> …/attribute::…

…/.… <=> …/self::node()…

…/..… <=> …/parent::node()…

…//… <=> …/descendant-or-self::node()/…

!!!!

Page 52: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

A predicate enables to specify advanced conditions for nodes which were selected by the axis and node test

For context node u we find all nodes selected by the axis from node u

On input we put those which satisfy node test and all predicates

axis::node-test predicate1 ... predicateN

predicate ::= ‘[‘ condition ‘]‘

condition ::= ‘not(‘ condition ‘)‘ |

condition ‘and‘ condition |

condition ‘or‘ condition

[condition1][condition2] <=> [condition1 and condition2]

Page 53: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

Predicate condition can be a relative XPath

path P

For node u it returns true if the set of nodes

returned by path P from u is non-empty

Predicated condition can be an absolute

XPath path P

It returns true if the set of nodes returned by path

P is non-empty

Page 54: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

//item[@code] Relative path: attribute::code

Page 55: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

//item[price] Relative path: child::price

Page 56: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

status items

item item

orders

... order

items

item item

//item[../../@status]

Page 57: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

status items

item item

orders

... order

items

item item

//item[ancestor::order/@status]

Page 58: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

status items

item item

orders

... order

items

item item

//order[@status]//item

Page 59: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

The condition can involve comparison of two

operands

Operands are XPath expressions

XPath path, value, ...

Operators are = != < > <= >=

String value of node

Attribute – normalized value

Element – concatenation of text nodes in its

subtree

Page 60: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

//item[color = "blue"]

Page 61: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

//item[price > 30]

Page 62: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

status items

item item

orders

... order

items

item item

"confirmed"

//order[@status = "confirmed"]//item

Page 63: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

Operators = != ...

Operands are sets of values/nodes

Evaluated as true if there exists a value/node in the left

operand and a value/node in the right operand for which

the operator evaluates as true

Consequences:

Expression with = and != can return the same result!

x="foo" is not the same as not(x!="foo")

There exists a node in x with string value foo

All nodes in x have string value foo

!!!

Page 64: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

color

text()

"yellow"

//item[color = "blue"]

Page 65: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

XPath Predicates

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

color

text()

"yellow"

//item[color != "blue"]

Page 66: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Built-in Functions

Testing of position

Each node in a context set has a position

Determined by its position in document and the

(direction of a) particular path

position()

Returns the position of node in a context set

last()

Returns the number of nodes in a context set

Page 67: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Built-in Functions

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

color

text()

"yellow"

//items/item[position() = 1]

Page 68: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Built-in Functions

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

color

text()

"yellow"

//items/item[1]

Page 69: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Built-in Functions

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

color

text()

"yellow"

//items/item[position() = last()]

Page 70: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Built-in Functions

order

document

date

"10/10/2008"

status

"confirmed"

customer

number

"C992"

text()

"Steve J."

items

item

code

"48282811"

amount price

text() text()

"5" "22"

item

code

"929118813"

amount color

text() text()

"1" "blue"

color

text()

"yellow"

//items/item[last()]

Page 71: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

Returns the number of results given by expression

Built-in Functions

count(expression)

Page 72: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

order

document

status items

item item

orders

... order

items

item item

"confirmed"

item

Built-in Functions

//items[count(item) > 2]

Page 73: XML, XPath€¦ · XPath, XQuery XSLT is a language for data transformation Exploits XPath for targeting parts of XML document Has XML syntax XQuery is more suitable for querying

+, -, *, div, mod

name(), id()

concat(), starts-with(),

contains(), substring-after(),

substring-before(), substring(),

...

sum(), floor(), ceiling(), ...

Built-in Functions


Recommended