Upload
thomas-weinert
View
1.108
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
#PBCAT
The Lumberjack - Xpath 101Thomas 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'));?>
boolean falsearray 0 => object(SimpleXMLElement)[2]
array empty
XSL
● Libxslt● based on Libxml2
● ext/xsl● ext/xslcache
Syntax
/element/child[@attr]
Absolute Path
Step 2
PredicateStep 1
Separator
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● ancestor-or-self● descendant-or-self● following● preceding● preceding-sibling● self
● attribute● namespaces
Short Syntax
Axis Shortchildself .parent ..attribute @descendant-or-self /
● self::node()/descendant-or-self::node()/child::para
● .//para
Cast Functions
● string()● number()● boolean()
echo $xpath->evaluate('string(/html/head/title)');
Node Functions
● count()● last()● position()
● name()● local-name()● namespace-uri()
$list = $xpath->evaluate( '//*[local-name() = 'li' and position() = last()]');
String Functions
● concat()● starts-with()● contains()● substring-before()● substring-after()● substring()● string-length()
● normalize-string()● translate()
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