xpath - a practical guide

32
XPath - A practical guide Arne Blankerts <[email protected]>, TobiasSchlitt <[email protected]> IPC 2009 2009-11-17 Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 1 / 26

Upload: tobias-schlitt

Post on 18-May-2015

5.655 views

Category:

Technology


1 download

DESCRIPTION

Slides from the XPath tutorial given by Arne Blankerts and me on the IPC 2009. We did a lot of life hacking during the session, so the slides are not that extensive.

TRANSCRIPT

Page 1: XPath - A practical guide

XPath - A practical guide

Arne Blankerts <[email protected]>, TobiasSchlitt <[email protected]>

IPC 2009

2009-11-17

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 1 / 26

Page 2: XPath - A practical guide

Outline

1 Welcome

2 Introduction

3 In details

4 Quiz

5 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 2 / 26

Page 3: XPath - A practical guide

Arne Blankerts

Arne Blankerts <[email protected]>

PHP since 1999 (10 years of PHP!)

Co-Founder of thePHP.cc

ballyhoo. werbeagentur.

Open source addicted

Inventor and lead developer of fCMS sitesystemContributor and translator for the PHP manual

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 3 / 26

Page 4: XPath - A practical guide

Tobias Schlitt

Tobias Schlitt <[email protected]>

PHP since 2001

Freelancing consultant

Qualified IT Specialist

Studying CS at TU Dortmund(finishing 2010)

OSS addicted

PHPeZ ComponentsPHPUnit

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 4 / 26

Page 5: XPath - A practical guide

Outline

1 Welcome

2 IntroductionOverviewBasics

3 In details

4 Quiz

5 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 5 / 26

Page 6: XPath - A practical guide

Overview

XPath

Enables you to select information parts from XML documents.

Traverse the XML tree

Select XML nodes

W3C recommendation

Version 1: November 1999

Version 2: January 2007

Fields of application

XSLT (XML Stylesheet Language Transformations)Fetching XML nodes within programming languages

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 6 / 26

Page 7: XPath - A practical guide

Addressing

Every XPath expression matches a set of nodes (0..n)

It encodes an “address” for the selected nodes

Simple XPath expressions look similar to Unix file system addresses

Absolute vs. relative addressing

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 7 / 26

Page 8: XPath - A practical guide

Practical

Let’s dig into the code

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 8 / 26

Page 9: XPath - A practical guide

Outline

1 Welcome

2 Introduction

3 In detailsSyntaxSpecialitiesFunctionsAxisPHP function integration

4 Quiz

5 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 9 / 26

Page 10: XPath - A practical guide

Syntax elements

/ Level seperator

// Multi-level seperator

* Wildcard

@ Attribute prefix

@* Attribute wildcard

[...] Filter / condition

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 10 / 26

Page 11: XPath - A practical guide

Contexts

Every expression step creates new context

Next evaluated in context created by previous step

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 11 / 26

Page 12: XPath - A practical guide

Navigation

/ One level down

// Any number of levels down

../ One level up

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 12 / 26

Page 13: XPath - A practical guide

Practical

Let’s dig into the code

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 13 / 26

Page 14: XPath - A practical guide

Indexing

Access nodes by position: [2]

Start index

Indexing generally 1 based

Some Internet Explorer versions start with 0

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 14 / 26

Page 15: XPath - A practical guide

Union

Union the node sets selected by multiple XPath expressions.

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 15 / 26

Page 16: XPath - A practical guide

Practical

Let’s dig into the code

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 16 / 26

Page 17: XPath - A practical guide

Functions

String

string-join()

substring()

starts-with()

contains()

string-length()

Math

round()

floor()

Context

position()

last()

Aggregate

count()

min() / max()

Node

local-name()

Logic

not()

true()

Function overview

An overview on all functions can be found onhttp://www.w3.org/TR/xpath-functions/

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 17 / 26

Page 18: XPath - A practical guide

Axis

13 dimensions

Imagine an XML document to be a 13 dimensional space...

Axis Shortcut

ancestor

ancestor-or-self

attribute @child -

descendant

descendant-or-self //following

following-sibling

namespace

parent ..preceding

preceding-sibling

self .

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 18 / 26

Page 19: XPath - A practical guide

Practical

Let’s dig into the code

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 19 / 26

Page 20: XPath - A practical guide

PHP function integration

Register PHP callbacks to enhance functionality

Possible since PHP 5.3.0

Works with functions and static methods

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 20 / 26

Page 21: XPath - A practical guide

Practical

Let’s dig into the code

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 21 / 26

Page 22: XPath - A practical guide

Outline

1 Welcome

2 Introduction

3 In details

4 Quiz

5 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 22 / 26

Page 23: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 24: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 25: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 26: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 27: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 28: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 29: XPath - A practical guide

Quiz

//node

/root/element

/root/element[1]

@id

/root//node/@name

/root/element[@name = ’php’]/@*.

./some/*[@class=’tek’]/@id

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 23 / 26

Page 30: XPath - A practical guide

Outline

1 Welcome

2 Introduction

3 In details

4 Quiz

5 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 24 / 26

Page 31: XPath - A practical guide

Q/A

Are there any questions left?

Please give us some feedback!

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 25 / 26

Page 32: XPath - A practical guide

The end

Thanks for being here!

We hope you enjoyed the session!

Slides and material

Delivered by Software & Supporthttp://schlitt.info/opensourceOn slideshare: http://www.slideshare.net/tobyS

Contact us:

Arne Blankerts <[email protected]>Tobias Schlitt <[email protected]>

Please rate our talk at: http://joind.in/1042

Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 26 / 26