xpath - a practical guide

Post on 18-May-2015

5.656 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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

XPath - A practical guide

Arne Blankerts <arne@thephp.cc>, TobiasSchlitt <toby@php.net>

IPC 2009

2009-11-17

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

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

Arne Blankerts

Arne Blankerts <arne@thephp.cc>

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

Tobias Schlitt

Tobias Schlitt <toby@php.net>

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

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

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

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

Practical

Let’s dig into the code

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

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

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

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

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

Practical

Let’s dig into the code

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

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

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

Practical

Let’s dig into the code

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

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

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

Practical

Let’s dig into the code

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

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

Practical

Let’s dig into the code

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

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

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

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

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

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

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

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

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

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

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

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 <arne@thephp.cc>Tobias Schlitt <toby@php.net>

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

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

top related