Cutting Edge Data Processing            with PHP & XQuery28   msec   William Candillon {candillon@28msec.com}            P...
A Rich Ecosystem       Platforms            Frameworks       LibrariesCodeData  PDO   PHP Data Objects   ?                ...
A Rich Ecosystem       Platforms            Frameworks      LibrariesCodeData  PDO   PHP Data Objects                     ...
Our Goal    Contribute the features fromrelational APIs to unstructured data
Data APIs              PDO        SimpleXML Zend JSON    XQuery  Data      Relational     XML       JSON      XML/JSONQuer...
Meet XQuery • Family of specifications from W3C   - XQuery 3.0   - Update Facility   - Scripting Extension   - Full-Text • ...
Meet Zorba         Open Source XQuery Processor         Contributors: Oracle, 28msec, FLWOR   All Flavors Available      R...
Zorba
<?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$xquery->importQuery(‘1+1’);echo $xquery->exe...
<?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$query = <<<‘XQ’  let $world := ‘World’  retu...
<?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$xquery->importQueryFromURI(‘hello.xq’);echo ...
<?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$query = <<<‘XQ’  declare variable $world ext...
$xquery = new XQueryProcessor();$query = <<<‘XQ’declare variable   $foo as xs:string external;declare variable   $bar as x...
QueriesUpdatesFull-TextStreaming
QueriesUpdatesFull-TextStreaming
import module namespace functx = "http://www.functx.com/";import module namespace html = "http://example.com/html";declare...
<div id=”sessions”>{  for $session in $sessions  let $title := string($session/@title)  where $session/@conf = “PHP Tour L...
<div id=”sessions”>{for $day-sessions in $sessionslet $starts := dateTime($day-sessions/@starts)let $day    := xs:day($sta...
QueriesUpdatesFull-TextStreaming
insert node $bios[@id=”wcandilllon”]       into $sessions[@id=”xquery”]
replace value of node $session/@starts        with “2011-11-24T10:15:00”replace value of node $session/@ends        with “...
let $session := $sessions[id="XQuery"]return  if ($session/rating) then    replace value of node $session/rating          ...
QueriesUpdatesFull-TextStreaming
let $sessions := $sessions/session[                   . contains text {$search-term}                   all words          ...
let $x := <msg>breakfast of champions</msg>return $x contains text "meal"
let $x := <msg>breakfast of champions</msg>return $x contains text "meal"  using thesaurus at "http://wordnet.princeton.ed...
let $doc := doc(“doc.xml”)for $token in ft:tokenize($doc)return  concat($token/@value, “ at ”,         $token/@paragraph, ...
Demo
QueriesUpdatesFull-TextStreaming
Forecast Data  - 1 Day of Forecast data: 727MB  - Get data for a specific site  - Send selected temperatures to clients  - ...
<?php$siteId = 3;$forecasts = simplexml_load_file(forecasts.xml);$forecasts = $forecasts->xpath(      "/forecast-list/fore...
<?php$siteId = 3;$forecasts = simplexml_load_file(forecasts.xml);$forecasts = $forecasts->xpath(      "/forecast-list/fore...
<?php$siteId = 3;$forecasts = simplexml_load_file(forecasts.xml);$forecasts = $forecasts->xpath(      "/forecast-list/fore...
for $forecast in z:parse-xml(file:read-text("forecasts.xml"),                             <opt:options>                   ...
Demo
Results                 SimpleXML XMLReader   XQuery   Streaming       X          ✔         ✔  Productivity     ✔         ...
Pubzone (2009)                                  Model              View                                  Controler        ...
AWS Libraries                                       Java               XQuery                    8589    Lines of code    ...
AWS Libraries                Java            XQuery                 13803                                         Lines of...
<html>  <head>    <script type=text/javascript>    function buy(e) {       newElement = document.createElement("p");      ...
<html xmlns="http://www.w3.org/1999/xhtml">   <head>    <script type=application/xquery>declare updating function local:bu...
http://www.youtube.com/watch?v=ql2TLTsilo8
XQuery in the Browser• Open Source Project from ETH• http://xqib.org• XQuery in the browser without a plug-in• Processor c...
! <script type="application/xquery">! declare updating function local:onclick(    $loc, $evtObj  ) {!   insert node <hr co...
<script type="text/javascript">foo = function (arg){        return the text was + arg;};</script><script type="application...
Demo
Wrap-Up Clip http://www.youtube.com/watch?v=6oY5ctVHEck
28   msec            Thank you!
Cutting Edge Data Processing with PHP & XQuery
Upcoming SlideShare
Loading in …5
×

Cutting Edge Data Processing with PHP & XQuery

13,131 views

Published on

The goal of this talk is to explore the gain of functionality and productivity that can be achieved by introducing XQuery into PHP applications.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
13,131
On SlideShare
0
From Embeds
0
Number of Embeds
6,614
Actions
Shares
0
Downloads
57
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Cutting Edge Data Processing with PHP & XQuery

  1. 1. Cutting Edge Data Processing with PHP & XQuery28 msec William Candillon {candillon@28msec.com} PHP Tour Lille 2011
  2. 2. A Rich Ecosystem Platforms Frameworks LibrariesCodeData PDO PHP Data Objects ? ? ? Relational XML JSON Text
  3. 3. A Rich Ecosystem Platforms Frameworks LibrariesCodeData PDO PHP Data Objects X Query Relational XML JSON Text
  4. 4. Our Goal Contribute the features fromrelational APIs to unstructured data
  5. 5. Data APIs PDO SimpleXML Zend JSON XQuery Data Relational XML JSON XML/JSONQueries ✔ XPath X ✔Updates ✔ X X ✔Streaming ✔ X X ✔Full-Text ✔ X X ✔
  6. 6. Meet XQuery • Family of specifications from W3C - XQuery 3.0 - Update Facility - Scripting Extension - Full-Text • EXPath: de-facto standard libraries
  7. 7. Meet Zorba Open Source XQuery Processor Contributors: Oracle, 28msec, FLWOR All Flavors Available Runs Everywhere Rich Module Libraries Developer Tools Pluggable Store Fun & Productive
  8. 8. Zorba
  9. 9. <?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$xquery->importQuery(‘1+1’);echo $xquery->execute();?>
  10. 10. <?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$query = <<<‘XQ’ let $world := ‘World’ return <h1>Hello {$world}</h1>XQ;$xquery->importQuery($query);echo $xquery->execute();?>
  11. 11. <?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$xquery->importQueryFromURI(‘hello.xq’);echo $xquery->execute();?>
  12. 12. <?phprequire_once ‘ZorbaXQueryProcessor.php’;$xquery = new XQueryProcessor();$query = <<<‘XQ’ declare variable $world external; <h1>Hello {$world}</h1>XQ;$xquery->importQuery($query);$xquery->setVariable(‘world’, ‘World!’);echo $xquery->execute();?>
  13. 13. $xquery = new XQueryProcessor();$query = <<<‘XQ’declare variable $foo as xs:string external;declare variable $bar as xs:integer external;declare variable $doc1 as document-node() external;declare variable $doc2 as document-node() external;$foo, $bar, $doc1, $doc2XQ;$xquery->importQuery($query);$xquery->setVariable(‘foo’, ‘bar’);$xquery->setVariable(‘bar’, 3);$doc = simplexml_load_file (data/sessions.xml);$xquery->setVariable("doc1", $doc);$doc = $xquery->parseXML ("<root />");$xquery->setVariable("doc2", $doc);echo $xquery->execute();
  14. 14. QueriesUpdatesFull-TextStreaming
  15. 15. QueriesUpdatesFull-TextStreaming
  16. 16. import module namespace functx = "http://www.functx.com/";import module namespace html = "http://example.com/html";declare variable $sessions external;<html lang="en"> <body> {$html:header} <div id="main">{ html:sessions($sessions/*) }</div> {$html:footer}</body></html>
  17. 17. <div id=”sessions”>{ for $session in $sessions let $title := string($session/@title) where $session/@conf = “PHP Tour Lille” order by $session/@starts ascending return <div> <h1>{$title}</h1> </div>}</div>
  18. 18. <div id=”sessions”>{for $day-sessions in $sessionslet $starts := dateTime($day-sessions/@starts)let $day := xs:day($starts)group by $dayorder by $starts ascendingreturn <div> <h1>{$day}</h1> { for $session in $day-sessions return <h2>{string($session/@title)}</h2> } </div>}</div>
  19. 19. QueriesUpdatesFull-TextStreaming
  20. 20. insert node $bios[@id=”wcandilllon”] into $sessions[@id=”xquery”]
  21. 21. replace value of node $session/@starts with “2011-11-24T10:15:00”replace value of node $session/@ends with “2011-11-24T10:15:00”
  22. 22. let $session := $sessions[id="XQuery"]return if ($session/rating) then replace value of node $session/rating with "B" else insert node <rating>B</rating> into $session
  23. 23. QueriesUpdatesFull-TextStreaming
  24. 24. let $sessions := $sessions/session[ . contains text {$search-term} all words distance at most 8 words]return if(empty($sessions)) then <h1>No Results found</h1> else html:sessions($sessions)
  25. 25. let $x := <msg>breakfast of champions</msg>return $x contains text "meal"
  26. 26. let $x := <msg>breakfast of champions</msg>return $x contains text "meal" using thesaurus at "http://wordnet.princeton.edu" relationship "narrower term"
  27. 27. let $doc := doc(“doc.xml”)for $token in ft:tokenize($doc)return concat($token/@value, “ at ”, $token/@paragraph, $token/@sentence )
  28. 28. Demo
  29. 29. QueriesUpdatesFull-TextStreaming
  30. 30. Forecast Data - 1 Day of Forecast data: 727MB - Get data for a specific site - Send selected temperatures to clients - Display chart
  31. 31. <?php$siteId = 3;$forecasts = simplexml_load_file(forecasts.xml);$forecasts = $forecasts->xpath( "/forecast-list/forecast[@site-id=$siteId]");foreach($forecasts as $forecast) { $time = $forecast->xpath("@time-step"); $value = $forecast->xpath( "//weather-elements/weather-element" ."[@name = ScreenTemperature]/text()");! echo "<temperature time=" .$time[0] ."value=" .$value[0]." />n";}?>
  32. 32. <?php$siteId = 3;$forecasts = simplexml_load_file(forecasts.xml);$forecasts = $forecasts->xpath( "/forecast-list/forecast[@site-id=$siteId]");foreach($forecasts as $forecast) { $time = $forecast->xpath("@time-step"); $value = $forecast->xpath( "//weather-elements/weather-element" ."[@name = ScreenTemperature]/text()");! echo "<temperature time=" .$time[0] Oups ?!? ."value=" .$value[0]." />n";}?>
  33. 33. <?php$siteId = 3;$forecasts = simplexml_load_file(forecasts.xml);$forecasts = $forecasts->xpath( "/forecast-list/forecast[@site-id=$siteId]");foreach($forecasts as $forecast) { $time = $forecast->xpath("@time-step"); $value = $forecast->xpath( "//weather-elements/weather-element" ."[@name = ScreenTemperature]/text()");! echo "<temperature time=" .$time[0] ."value=" .$value[0]." />n"; 9GB}?>Memory Footprint:
  34. 34. for $forecast in z:parse-xml(file:read-text("forecasts.xml"), <opt:options> <opt:parseExternalParsedEntity opt:skipRootNodes="1"/> </opt:options>)where $forecast/@site-id = "3"let $time := string($forecast/@time-step)let $value := $forecast/weather-elements/weather-element [@name = ScreenTemperature]/text()return <temperature time="{$time}" value="{$value}" />Memory Footprint: 19MB
  35. 35. Demo
  36. 36. Results SimpleXML XMLReader XQuery Streaming X ✔ ✔ Productivity ✔ X ✔
  37. 37. Pubzone (2009) Model View Controler 4100 Lines of code 3100 1830 1210 900 450 Java XQuery
  38. 38. AWS Libraries Java XQuery 8589 Lines of code 2905 2309 1469 572 455 S3 SimpleDB SNS
  39. 39. AWS Libraries Java XQuery 13803 Lines of CodesLines of code - 80% 2496 AWS
  40. 40. <html> <head> <script type=text/javascript> function buy(e) { newElement = document.createElement("p"); elementText = document.createTextNode (e.target.getAttribute(id)); newElement.appendChild(elementText); var res = document.evaluate( "//div[@id=shoppingcart]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, HTML null); res.snapshotItem(0).appendChild("newElement");} </script> JavaScript </head> <body> <div>Shopping cart</div> <div id="shoppingcart"></div> <% XPath // Code establishing connection ResultSet results = statement.executeQuery ("SELECT * FROM PRODUCTS"); while (results.next()) { out.println("<div>"); Java String prodName = results.getString(1); out.println(prodName); out.println("<input type=button value=Buy"); out.println("id="+prodName+""); SQL out.println("onclick=buy(event)/>"). out.println("</div>"); } results.close(); // Code closing connection %> </body></html>
  41. 41. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type=application/xquery>declare updating function local:buy($evt, $obj) { insert node <p>{$obj/@id}</p> as first into //div[@id="shoppingcart"]};b:addEventListener(b:dom()//input, "onclick", xs:Qname("local:buy"));</script> </head> <body> <div>Shopping cart</div> <div id="shoppingcart">{ XQuery Everywhere for $p in doc("products.xml")//*:product return <div> {$p/*:name} <input type=button value=Buy id={$p/*:name}/> </div> }</div> </body></html>
  42. 42. http://www.youtube.com/watch?v=ql2TLTsilo8
  43. 43. XQuery in the Browser• Open Source Project from ETH• http://xqib.org• XQuery in the browser without a plug-in• Processor compiled to JavaScript• DOM as the processor store
  44. 44. ! <script type="application/xquery">! declare updating function local:onclick( $loc, $evtObj ) {! insert node <hr color="red"/>! as last into b:dom()//body! }; b:addEventListener( b:dom()//input, "onclick", local:onclick#2 )! </script>
  45. 45. <script type="text/javascript">foo = function (arg){ return the text was + arg;};</script><script type="application/xquery">! let $x := b:js-call(window.foo, “Foo”)! return b:alert($x)</script>
  46. 46. Demo
  47. 47. Wrap-Up Clip http://www.youtube.com/watch?v=6oY5ctVHEck
  48. 48. 28 msec Thank you!

×