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 site
                 system
                 Contributor 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
                 PHP
                 eZ Components
                 PHPUnit




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



 1 Welcome


 2 Introduction
          Overview
          Basics

 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 details
          Syntax
          Specialities
          Functions
          Axis
          PHP 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                                                  Context
         string-join()                                           position()
         substring()                                             last()
         starts-with()                                   Aggregate
         contains()                                              count()
         string-length()                                         min() / max()
 Math                                                    Node
         round()                                                 local-name()
         floor()                                         Logic
                                                                 not()
                                                                 true()
  Function overview
  An overview on all functions can be found on
  http://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 & Support
                  http://schlitt.info/opensource
                  On 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

XPath - A practical guide

  • 1.
    XPath - Apractical 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
  • 2.
    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
  • 3.
    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 site system Contributor and translator for the PHP manual Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 3 / 26
  • 4.
    Tobias Schlitt Tobias Schlitt <toby@php.net> PHP since 2001 Freelancing consultant Qualified IT Specialist Studying CS at TU Dortmund (finishing 2010) OSS addicted PHP eZ Components PHPUnit Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 4 / 26
  • 5.
    Outline 1 Welcome 2 Introduction Overview Basics 3 In details 4 Quiz 5 The end Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 5 / 26
  • 6.
    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
  • 7.
    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
  • 8.
    Practical Let’s diginto the code Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 8 / 26
  • 9.
    Outline 1 Welcome 2 Introduction 3 In details Syntax Specialities Functions Axis PHP function integration 4 Quiz 5 The end Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 9 / 26
  • 10.
    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
  • 11.
    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
  • 12.
    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
  • 13.
    Practical Let’s diginto the code Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 13 / 26
  • 14.
    Indexing Accessnodes 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
  • 15.
    Union Unionthe node sets selected by multiple XPath expressions. Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 15 / 26
  • 16.
    Practical Let’s diginto the code Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 16 / 26
  • 17.
    Functions String Context string-join() position() substring() last() starts-with() Aggregate contains() count() string-length() min() / max() Math Node round() local-name() floor() Logic not() true() Function overview An overview on all functions can be found on http://www.w3.org/TR/xpath-functions/ Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 17 / 26
  • 18.
    Axis 13dimensions 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
  • 19.
    Practical Let’s diginto the code Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 19 / 26
  • 20.
    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
  • 21.
    Practical Let’s diginto the code Arne Blankerts, Tobias Schlitt (IPC 2009) XPath - A practical guide 2009-11-17 21 / 26
  • 22.
    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
  • 23.
    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
  • 24.
    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
  • 25.
    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
  • 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
  • 27.
    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
  • 28.
    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
  • 29.
    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
  • 30.
    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
  • 31.
    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
  • 32.
    The end Thanks for being here! We hope you enjoyed the session! Slides and material Delivered by Software & Support http://schlitt.info/opensource On 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