Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

XPath Refresher

A refresher introduction to XPath for the eXist-db Summer School in Maynooth 29 August 2015. License: CC+by; http://tinyurl.com/jc-xpath-maynooth
by

James Cummings

on 11 November 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of XPath Refresher

<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
Really attributes
and segments of text are also separate nodes
/body/div/head
XPath locates
any matching
nodes
/body/div/lg
/body/div/@type
@ = attributes
/body/div/lg/l
/body/div/lg/l[@n="2"]
Square brackets
filter selection
/body/div[@type="poem"]/head
//lg[@type="stanza"]
// = any descendant
//div[@type="poem"]//l
//lg/../@type
.. = parent
//l[number(@n) > 5]
number() turns
strings into
numbers
//div[lg/@type='couplet']/@type
//l[ancestor::div/@type='shortPoem']
ancestor:: is an
unabbreviated
axis name
About XPath
XPath is the basis of most other XML querying and transformation languages (such as XSLT and XQuery). It is used in many other contexts (Editors, XML Databases, Content Management Systems, Configuration Files) as a way to say what bit(s) of an XML document you are referring to.
What does XPath
do
?
XPath does nothing! It merely documents a set of nodes. An XPath processor does something with this.
An XPath expression matches a set of nodes
It encodes an 'address' for the selected nodes, sort of like a URL does or a file path
This address can be given in a variety of ways including absolute or relative methods
It is just a way of locating nodes in an XML document.
<
body

n
="
anthology
">
<
div

type
="
poem
">
<
head
>The SICK ROSE</
head
>
<
lg

type
="
stanza
">
<
l

n
="
1
">O Rose thou art sick.</
l
>
<
l

n
="
2
">The invisible worm,</
l
>
<
l

n
="
3
">That flies in the night </
l
>
<
l

n
="
4
">In the howling storm:</
l
>
</
lg
>
<
lg

type
="
stanza
">
<
l

n
="
5
">Has found out thy bed </
l
>
<
l

n
="
6
">Of crimson joy:</
l
>
<
l

n
="
7
">And his dark secret love </
l
>
<
l

n
="
8
">Does thy life destroy.</
l
>
</
lg
>
</
div
>
<
div

type
="
shortPoem
">
<
head
>Amorous Rhyme</
head
>
<
lg

type
="
couplet
">
<
l

n
="
1
">What inspired this amorous rhyme?</
l
>
<
l

n
="
2
">Two parts vodka, one part lime</
l
>
</
lg
>
</
div
>
</
body
>
Poetry Example
/
Level selector
//

Multi-level selector
../
One level up (relative)
*

Wildcard
@
Attribute prefix
@*
Attribute wildcard
[...]
Filter / condition
function(item, 'parameter')
function with parameters
Basic XPath
Abbreviated Syntax
Every expression 'step' creates a new context, narrowing down the choice (usually) as you go further down the path.
More About XPaths
A location path results in a node-set
Paths can be absolute (
/div/lg[1]/l
)
Paths can be relative (
l/../../head
)
Formal Syntax: (
axisname::nodetest[predicate]
)
Predicates can nest
child::div[contains(upper-case(head), 'ROSE')]
ancestor::
ancestors (parent, grandparent, etc.) of the current node

ancestor-or-self::
the current node plus all its ancestors (parent, grandparent, etc.)

attribute::
attributes of the current node

child::
children of the current node

descendant::
descendants (children, grandchildren, etc.) of the current node

descendant-or-self::
the current node plus all its descendants (children, grandchildren, etc.)
XPath Axes 1
following::
everything in the document after the closing tag of the current node

following-sibling::
all siblings after the current node

parent::
the parent of the current node

preceding::
everything in the document that is before the starting tag of the current node

preceding-sibling::
Contains all siblings before the current node

self::
the current node
XPath Axes 2
child::lg[attribute::type='stanza']
child::l[@n='4']
child::div[position()=3]
child::div[4]
child::l[last()]
child::lg[last()-1]
XPath
Predicates
XPath contains numerous functions including:

Conversion:
boolean, string, number
Contexts:
deep-equal, last, position
DateTimes:
current-dateTime, day-from-dateTime, months-from-duration, timezone-from-dateTime
Math:
avg, ceiling, count, floor, max, min, round, sum
Logic:
true, false, not
Nodes:
lang, local-name, name, namespace-uri, text
Sequences:
distinct-values, empty, index-of, subsequence
Strings:
concat, contains, lower-case, matches,
normalize-space, replace, starts-with, string-length, substring, substring-after, substring-before, tokenize, translate, upper-case
XPath Functions
Now we're going to do an exercise!
Reference:
http://www.w3schools.com/xpath
http://www.w3schools.com/xpath/xpath_functions.asp
Experiment with your own files!
XPath
@jamescummings
http://tinyurl.com/jc-xpath-maynooth
XML Structure
<
body

type
="
anthology
">
<
div

type
="
poem
">
<
div

type
="
shortPoem
">
<
head
>
<
head
>
<
lg

type
="
couplet
">
<
lg

type
="
stanza
">
<
l

n
="
1
">
<
l

n
="
2
">
<
l

n
="
3
">
<
l

n
="
4
">
<
lg

type
="
stanza
">
<
l

n
="
5
">
<
l

n
="
6
">
<
l

n
="
7
">
<
l

n
="
8
">
<
l

n
="
1
">
<
l

n
="
2
">
//div[.//l/@n='5']
. sets the context
for our //l to inside
the current 'div'
Exercise!
Dr James Cummings
University of Oxford

|

Union of two node-sets
(//name | //placeName)
+

Addition
(5 + 4)
-

Subtraction
(last() - 1) or (5 - 4)
*

Multiplication
(6 * 4)
div

Division
(8 div 4)
=

Equal
(@value = '1')
!=

Not equal
(@rend != 'italic')
<

Less than
(@value < 10)
<=

Less than or equal to
(@value <= 2)
>

Greater than
(@value > 2)
>=

Greater than or equal to
(@value >= 3)
or

or
(@rend='italic' or @rend='it')
and

and
(@rend='italic' and @type='exit')
mod

Modulus (division remainder)
(5 mod 2)
XPath
Operators
//person[sex/@value='2']/persName
Full transcript