SlideShare una empresa de Scribd logo
1 de 27
Descargar para leer sin conexión
#PBCAT




     The Lumberjack - Xpath 101
            Thomas Weinert
About Me
●   Application Developer
    ●   PHP
    ●   JavaScript
    ●   XSL
●   papaya Software GmbH
    ●   papaya CMS
    ●   Technical Director
●   FluentDOM
Questions!




         Please ask any time!
Xpath 1
●   XML Path Language
●   W3C Recommendation 16 November 1999
●   Used by
    ●   XSLT 1
    ●   XPointer
Xpath 2
●   W3C Recommendation 23 January 2007
●   Superset of Xpath 1
●   More data types
DOM
●   Document Object Modell
●   Standard extension: ext/dom
●   LibXml2
    ●   Xpath 1
DOMXpath
●   Create after loading the document!
●   evaluate()/query()
<?php
   $str = '<sample><element/></sample>';
   $dom = new DOMDocument();
   $dom->loadXML($str);
   $xpath = new DOMXPath($dom);
   var_dump($xpath->evaluate('//element'));
   var_dump($xpath->evaluate('//noelement'));
   var_dump($xpath->evaluate('//noelement/@attr'));
?>


                   object(DOMNodeList)[5]
SimpleXML
●    Always return SimpleXML
<?php
  $str = '<sample><element/></sample>';
  $xml = simplexml_load_string($str);

    var_dump($xml->xpath('//element'));
    var_dump($xml->xpath('//noelement'));

    var_dump($xml->xpath('//noelement/@attr'));
?>


 array                            array     boolean false
  0 =>                             empty
    object(SimpleXMLElement)[2]
XSL
●   Libxslt
    ●   based on Libxml2
●   ext/xsl
●   ext/xslcache
Syntax

                    /element/child[@attr]

    Absolute Path     Step 1               Predicate




                           Separator   Step 2
Nodes
●   node()
    ●   * or qualified-name
    ●   text()
    ●   comment()
    ●   processing-instruction()
Axes
●   axis::...
●   Full syntax
●   Short Syntax
●   Default Axis
child
<barcamps>
  <barcamp title="PHP Unconference Hamburg" id="phpuchh">
    <link href="http://www.php-unconference.de/" />
  </barcamp>
  <barcamp title="PHP Barcamp Salzburg" id="phpbcat">
    <link href="http://www.phpbarcamp.at/cms/" />
    <speakers-featured>
      <speaker>Bastian Feder</speaker>
    </speakers-featured>
    <speakers>
      <speaker>Thomas Weinert</speaker>
    </speakers>
  </barcamp>
  <barcamp title="PHP Unconference Europe" id="phpuceu">
    <link href="http://www.phpuceu.org/">
  </barcamp>
</barcamps>
descendant
<barcamps>
  <barcamp title="PHP Unconference Hamburg" id="phpuchh">
    <link href="http://www.php-unconference.de/" />
  </barcamp>
  <barcamp title="PHP Barcamp Salzburg" id="phpbcat">
    <link href="http://www.phpbarcamp.at/cms/" />
    <speakers-featured>
      <speaker>Bastian Feder</speaker>
    </speakers-featured>
    <speakers>
      <speaker>Thomas Weinert</speaker>
    </speakers>
  </barcamp>
  <barcamp title="PHP Unconference Europe" id="phpuceu">
    <link href="http://www.phpuceu.org/">
  </barcamp>
</barcamps>
parent
<barcamps>
  <barcamp title="PHP Unconference Hamburg" id="phpuchh">
    <link href="http://www.php-unconference.de/" />
  </barcamp>
  <barcamp title="PHP Barcamp Salzburg" id="phpbcat">
    <link href="http://www.phpbarcamp.at/cms/" />
    <speakers-featured>
      <speaker>Bastian Feder</speaker>
    </speakers-featured>
    <speakers>
      <speaker>Thomas Weinert</speaker>
    </speakers>
  </barcamp>
  <barcamp title="PHP Unconference Europe" id="phpuceu">
    <link href="http://www.phpuceu.org/">
  </barcamp>
</barcamps>
following-sibling
<barcamps>
  <barcamp title="PHP Unconference Hamburg" id="phpuchh">
    <link href="http://www.php-unconference.de/" />
  </barcamp>
  <barcamp title="PHP Barcamp Salzburg" id="phpbcat">
    <link href="http://www.phpbarcamp.at/cms/" />
    <speakers-featured>
      <speaker>Bastian Feder</speaker>
    </speakers-featured>
    <speakers>
      <speaker>Thomas Weinert</speaker>
    </speakers>
  </barcamp>
  <barcamp title="PHP Unconference Europe" id="phpuceu">
    <link href="http://www.phpuceu.org/">
  </barcamp>
</barcamps>
More Axes
●   ancestor             ●   attribute
●   ancestor-or-self     ●   namespaces
●   descendant-or-self
●   following
●   preceding
●   preceding-sibling
●   self
Short Syntax
●   self::node()/
    descendant-or-self::node()/
    child::para
●   .//para

                 Axis             Short
child
self                       .
parent                     ..
attribute                  @
descendant-or-self         /
Cast Functions
●   string()
●   number()
●   boolean()

    echo $xpath->evaluate('string(/html/head/title)');
Node Functions
●   count()                 ●   name()
●   last()                  ●   local-name()
●   position()              ●   namespace-uri()



$list = $xpath->evaluate(
  '//*[local-name() = 'li' and position() = last()]'
);
String Functions
●   concat()             ●   normalize-string()
●   starts-with()        ●   translate()
●   contains()
●   substring-before()
●   substring-after()
●   substring()
●   string-length()
Match A Class
●   normalize-string()
●   concat()
●   contains()
Namespaces
●   URN
●   Prefix
●   Default Namespace
●   Own Prefixes
●   Attributes
Bug #49490
●   Namespace prefix conflict
$dom = new DOMDocument();
$dom->loadXML(
   '<foobar><a:foo xmlns:a="urn:a">'.
   '<b:bar xmlns:b="urn:b"/></a:foo>'.
   '</foobar>'
);
$xpath = new DOMXPath($dom);
$context = $dom->documentElement->firstChild;
$xpath->registerNamespace('a', 'urn:b');
var_dump(
   $xpath->evaluate('descendant-or-self::a:*', $context)
     ->item(0)->tagName
);
Tools
●   Firebug
●   Firefox AddOns
CSS Selectors
●   JavaScript libraries
●   element nodes
    ●   *
●   no axes
    ●   descendant-or-self::*
●   can ignore namespaces
    ●   descendant-or-self::*[local-name() = '...']
Thanks
●   Web:
    ●   http://www.papaya-cms.com/
    ●   http://www.a-basketful-of-papayas.net/
●   Twitter
    ●   @ThomasWeinert
●   Joind.in
    ●   http://joind.in/1621

Más contenido relacionado

La actualidad más candente

Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
taggg
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 

La actualidad más candente (20)

PHP7 Presentation
PHP7 PresentationPHP7 Presentation
PHP7 Presentation
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Php
PhpPhp
Php
 
Extending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockExtending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh Pollock
 
An Introduction to Symfony
An Introduction to SymfonyAn Introduction to Symfony
An Introduction to Symfony
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
A new way to develop with WordPress!
A new way to develop with WordPress!A new way to develop with WordPress!
A new way to develop with WordPress!
 
Php basics
Php basicsPhp basics
Php basics
 
Php
PhpPhp
Php
 
Thymeleaf Introduction
Thymeleaf IntroductionThymeleaf Introduction
Thymeleaf Introduction
 
Datagrids with Symfony 2, Backbone and Backgrid
Datagrids with Symfony 2, Backbone and BackgridDatagrids with Symfony 2, Backbone and Backgrid
Datagrids with Symfony 2, Backbone and Backgrid
 
Introducere in web
Introducere in webIntroducere in web
Introducere in web
 
Asynchronous programming patterns in Perl
Asynchronous programming patterns in PerlAsynchronous programming patterns in Perl
Asynchronous programming patterns in Perl
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 

Destacado (7)

Optimizing Your Frontend Performance
Optimizing Your Frontend PerformanceOptimizing Your Frontend Performance
Optimizing Your Frontend Performance
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
 
Deliver Files With PHP
Deliver Files With PHPDeliver Files With PHP
Deliver Files With PHP
 
Controlling Arduino With PHP
Controlling Arduino With PHPControlling Arduino With PHP
Controlling Arduino With PHP
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHP
 
PHPUG CGN: Controlling Arduino With PHP
PHPUG CGN: Controlling Arduino With PHPPHPUG CGN: Controlling Arduino With PHP
PHPUG CGN: Controlling Arduino With PHP
 
XPath Introduction
XPath IntroductionXPath Introduction
XPath Introduction
 

Similar a Lumberjack XPath 101

GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
Nat Weerawan
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
Hugo Hamon
 
GHCソースコード読みのススメ
GHCソースコード読みのススメGHCソースコード読みのススメ
GHCソースコード読みのススメ
Kiwamu Okabe
 

Similar a Lumberjack XPath 101 (20)

Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
Network Automation: Ansible 102
Network Automation: Ansible 102Network Automation: Ansible 102
Network Automation: Ansible 102
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
 
Building Advanced XSS Vectors
Building Advanced XSS VectorsBuilding Advanced XSS Vectors
Building Advanced XSS Vectors
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
XPath for web scraping
XPath for web scrapingXPath for web scraping
XPath for web scraping
 
The beautyandthebeast phpbat2010
The beautyandthebeast phpbat2010The beautyandthebeast phpbat2010
The beautyandthebeast phpbat2010
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
PHP Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance Trivia
 
PSR-7 and PSR-15, why can't you ignore them
PSR-7 and PSR-15, why can't you ignore themPSR-7 and PSR-15, why can't you ignore them
PSR-7 and PSR-15, why can't you ignore them
 
Ant vs Phing
Ant vs PhingAnt vs Phing
Ant vs Phing
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
GHCソースコード読みのススメ
GHCソースコード読みのススメGHCソースコード読みのススメ
GHCソースコード読みのススメ
 
PHP Development Tools
PHP  Development ToolsPHP  Development Tools
PHP Development Tools
 

Más de Thomas Weinert (8)

FluentDom
FluentDomFluentDom
FluentDom
 
Optimizing Your Frontend Performance
Optimizing Your Frontend PerformanceOptimizing Your Frontend Performance
Optimizing Your Frontend Performance
 
Experiences With Pre Commit Hooks
Experiences With Pre Commit HooksExperiences With Pre Commit Hooks
Experiences With Pre Commit Hooks
 
The Lumber Mill - XSLT For Your Templates
The Lumber Mill  - XSLT For Your TemplatesThe Lumber Mill  - XSLT For Your Templates
The Lumber Mill - XSLT For Your Templates
 
The Lumber Mill Xslt For Your Templates
The Lumber Mill   Xslt For Your TemplatesThe Lumber Mill   Xslt For Your Templates
The Lumber Mill Xslt For Your Templates
 
SVN Hook
SVN HookSVN Hook
SVN Hook
 
PHP 5.3/6
PHP 5.3/6PHP 5.3/6
PHP 5.3/6
 
Optimizing Your Frontend Performance
Optimizing Your Frontend PerformanceOptimizing Your Frontend Performance
Optimizing Your Frontend Performance
 

Lumberjack XPath 101

  • 1. #PBCAT The Lumberjack - Xpath 101 Thomas Weinert
  • 2. About Me ● Application Developer ● PHP ● JavaScript ● XSL ● papaya Software GmbH ● papaya CMS ● Technical Director ● FluentDOM
  • 3. Questions! Please ask any time!
  • 4. Xpath 1 ● XML Path Language ● W3C Recommendation 16 November 1999 ● Used by ● XSLT 1 ● XPointer
  • 5. Xpath 2 ● W3C Recommendation 23 January 2007 ● Superset of Xpath 1 ● More data types
  • 6. DOM ● Document Object Modell ● Standard extension: ext/dom ● LibXml2 ● Xpath 1
  • 7. DOMXpath ● Create after loading the document! ● evaluate()/query() <?php $str = '<sample><element/></sample>'; $dom = new DOMDocument(); $dom->loadXML($str); $xpath = new DOMXPath($dom); var_dump($xpath->evaluate('//element')); var_dump($xpath->evaluate('//noelement')); var_dump($xpath->evaluate('//noelement/@attr')); ?> object(DOMNodeList)[5]
  • 8. SimpleXML ● Always return SimpleXML <?php $str = '<sample><element/></sample>'; $xml = simplexml_load_string($str); var_dump($xml->xpath('//element')); var_dump($xml->xpath('//noelement')); var_dump($xml->xpath('//noelement/@attr')); ?> array array boolean false 0 => empty object(SimpleXMLElement)[2]
  • 9. XSL ● Libxslt ● based on Libxml2 ● ext/xsl ● ext/xslcache
  • 10. Syntax /element/child[@attr] Absolute Path Step 1 Predicate Separator Step 2
  • 11. Nodes ● node() ● * or qualified-name ● text() ● comment() ● processing-instruction()
  • 12. Axes ● axis::... ● Full syntax ● Short Syntax ● Default Axis
  • 13. child <barcamps> <barcamp title="PHP Unconference Hamburg" id="phpuchh"> <link href="http://www.php-unconference.de/" /> </barcamp> <barcamp title="PHP Barcamp Salzburg" id="phpbcat"> <link href="http://www.phpbarcamp.at/cms/" /> <speakers-featured> <speaker>Bastian Feder</speaker> </speakers-featured> <speakers> <speaker>Thomas Weinert</speaker> </speakers> </barcamp> <barcamp title="PHP Unconference Europe" id="phpuceu"> <link href="http://www.phpuceu.org/"> </barcamp> </barcamps>
  • 14. descendant <barcamps> <barcamp title="PHP Unconference Hamburg" id="phpuchh"> <link href="http://www.php-unconference.de/" /> </barcamp> <barcamp title="PHP Barcamp Salzburg" id="phpbcat"> <link href="http://www.phpbarcamp.at/cms/" /> <speakers-featured> <speaker>Bastian Feder</speaker> </speakers-featured> <speakers> <speaker>Thomas Weinert</speaker> </speakers> </barcamp> <barcamp title="PHP Unconference Europe" id="phpuceu"> <link href="http://www.phpuceu.org/"> </barcamp> </barcamps>
  • 15. parent <barcamps> <barcamp title="PHP Unconference Hamburg" id="phpuchh"> <link href="http://www.php-unconference.de/" /> </barcamp> <barcamp title="PHP Barcamp Salzburg" id="phpbcat"> <link href="http://www.phpbarcamp.at/cms/" /> <speakers-featured> <speaker>Bastian Feder</speaker> </speakers-featured> <speakers> <speaker>Thomas Weinert</speaker> </speakers> </barcamp> <barcamp title="PHP Unconference Europe" id="phpuceu"> <link href="http://www.phpuceu.org/"> </barcamp> </barcamps>
  • 16. following-sibling <barcamps> <barcamp title="PHP Unconference Hamburg" id="phpuchh"> <link href="http://www.php-unconference.de/" /> </barcamp> <barcamp title="PHP Barcamp Salzburg" id="phpbcat"> <link href="http://www.phpbarcamp.at/cms/" /> <speakers-featured> <speaker>Bastian Feder</speaker> </speakers-featured> <speakers> <speaker>Thomas Weinert</speaker> </speakers> </barcamp> <barcamp title="PHP Unconference Europe" id="phpuceu"> <link href="http://www.phpuceu.org/"> </barcamp> </barcamps>
  • 17. More Axes ● ancestor ● attribute ● ancestor-or-self ● namespaces ● descendant-or-self ● following ● preceding ● preceding-sibling ● self
  • 18. Short Syntax ● self::node()/ descendant-or-self::node()/ child::para ● .//para Axis Short child self . parent .. attribute @ descendant-or-self /
  • 19. Cast Functions ● string() ● number() ● boolean() echo $xpath->evaluate('string(/html/head/title)');
  • 20. Node Functions ● count() ● name() ● last() ● local-name() ● position() ● namespace-uri() $list = $xpath->evaluate( '//*[local-name() = 'li' and position() = last()]' );
  • 21. String Functions ● concat() ● normalize-string() ● starts-with() ● translate() ● contains() ● substring-before() ● substring-after() ● substring() ● string-length()
  • 22. Match A Class ● normalize-string() ● concat() ● contains()
  • 23. Namespaces ● URN ● Prefix ● Default Namespace ● Own Prefixes ● Attributes
  • 24. Bug #49490 ● Namespace prefix conflict $dom = new DOMDocument(); $dom->loadXML( '<foobar><a:foo xmlns:a="urn:a">'. '<b:bar xmlns:b="urn:b"/></a:foo>'. '</foobar>' ); $xpath = new DOMXPath($dom); $context = $dom->documentElement->firstChild; $xpath->registerNamespace('a', 'urn:b'); var_dump( $xpath->evaluate('descendant-or-self::a:*', $context) ->item(0)->tagName );
  • 25. Tools ● Firebug ● Firefox AddOns
  • 26. CSS Selectors ● JavaScript libraries ● element nodes ● * ● no axes ● descendant-or-self::* ● can ignore namespaces ● descendant-or-self::*[local-name() = '...']
  • 27. Thanks ● Web: ● http://www.papaya-cms.com/ ● http://www.a-basketful-of-papayas.net/ ● Twitter ● @ThomasWeinert ● Joind.in ● http://joind.in/1621