SlideShare a Scribd company logo
High Performance XQuery
Processing in PHP with Zorba




                               Vikram Vaswani
                               November 2012
Vikram Vaswani
Founder, Melonfire
●   Product design and implementation with open source technologies
●   20+ products launched for customers worldwide
Author and active PHP community participant
●   7 books for McGraw-Hill USA
●   250+ tutorials for IBM dW, Zend Developer Zone and others

More information at http://vikram-vaswani.in
XML Support in PHP
●   XML processing: SimpleXML and XMLReader
●   XML transformation: XSLT and XML-DAS
●   Event-based parsing: SAX
●   Tree-based parsing: DOM
●   Serialization: WDDX
●   Dynamic documents: XMLWriter
What's Missing?
Introducing Zorba
●   General purpose XQuery processor for PHP 5.3+
●   Supports XQuery v1.0 and related specifications
●   Available for Windows, Linux, and Mac OS X
●   Open source; Apache 2.0 License
●   Project supported by FLWOR Foundation and
    Oracle.
Supported XML Technologies
●   XPath
●   XQuery v1.0 and v3.0
●   XQuery Scripting
●   Xquery Data Definition
●   XPath Full-Text 1.0
●   XSLT 1.0
●   XqueryX (limited)
PHP Installation
●   Linux and MacOS X
    ●   Libxml2: http://xmlsoft.org/
    ●   Iconv: http://www.gnu.org/software/libiconv/
    ●   libUUID: http://linux.die.net/man/3/libuuid
    ●   ICU (C version): http://www.icu-project.org/
    ●   Xerces (C version): http://xerces.apache.org/xerces-c/
    ●   GCC and related make/cmake utilities
●   Windows
    ●   https://launchpad.net/zorba/+download
●   API Wrapper
    ●   http://zorba.s3.amazonaws.com/ZorbaPHP.zip
PHP Installation
●   Linux and MacOS X
    ●   Libxml2: http://xmlsoft.org/
    ●   Iconv: http://www.gnu.org/software/libiconv/
    ●   libUUID: http://linux.die.net/man/3/libuuid
    ●   ICU (C version): http://www.icu-project.org/
    ●   Xerces (C version): http://xerces.apache.org/xerces-c/
    ●   GCC and related make/cmake utilities
●   Windows
    ●   https://launchpad.net/zorba/+download
●   API Wrapper
    ●   http://zorba.s3.amazonaws.com/ZorbaPHP.zip
Basic Usage
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
try {
    $queryStr = <<<END
    let $message := 'She sells sea shells by the sea shore'
    return
    <result>{$message}</result>
END;
    $query = $zorba->importQuery($queryStr);
    $result = $zorba->execute();
    echo $result;
} catch (Exception $e) {
    die('ERROR:' . $e->getMessage());
}
                                    Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                            http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Basic Usage
Usage Examples
Arithmetic Operators
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<<END
  declare variable $x := 30;
  declare variable $y := 12;
  let $r := $x + $y
  return
  <result> {$r} </result>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                             Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                     http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Sequences
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<<END
 let $dolls := ('Echo', 'Victor', 'Sierra', 'November', 'Alpha')
 return
 <results>
 {
     for $doll in $dolls
     return
     <name>{$doll}</name>
 }
 </results>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                                    Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                            http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
String Functions
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<<END
  let $dolls := ('Echo', 'Victor', 'Sierra', 'November', 'Alpha')
  return
  <results>
  {string-join($dolls, ', ')}
  </results>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();




                                  Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                          http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Math Functions
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<<END
 let $set := (1 to 20)
 return
 <results>
 {
       for $i in $set
       where ($i mod 2 eq 0)
       return
       <value> {$i} </value>
 }
 </results>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();

                                           Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                                   http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Data Filtering
(XML source)
<?xml version="1.0"?>
<data>
  <record>
    <code>ABW</code>
    <name>Aruba</name>
    <continent>North America</continent>
    <year></year>
    <population>103000</population>
    <gnp>828.00</gnp>
    <capital>
        <name>Oranjestad</name>
        <population>29034</population>
    </capital>
  </record>
  ...
</data>                                  Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                                 http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Data Filtering (Simple)
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$dm = $zorba->getXmlDataManager();
$dm->loadDocument('data.xml', file_get_contents('data.xml'));
$queryStr = <<< END
 for $record in doc('data.xml')//record
 let $name := $record/name/text()
 let $code := $record/code/text()
 return
 <name code="{$code}">{$name}</name>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                                  Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                          http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Data Filtering (Complex)
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
dm = $zorba->getXmlDataManager();
$dm->loadDocument('data.xml', file_get_contents('data.xml'));
$queryStr = <<< END
 for $record in doc('data.xml')//record
 let $name := $record/name/text()
 let $code := $record/code/text()
 let $population := $record/population/text()
 where contains($record/continent/text(), 'Europe')
 where (xs:integer($population) gt 1000000)
 order by $population descending
 return
 <name code="{$code}" population="{$population}">{$name}</name>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                                            Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                                    http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Node Manipulation
(XML source, before)
<?xml version='1.0'?>
<heroes>
 <hero>
   <name>Spiderman</name>
   <alterego>Peter Parker</alterego>
 </hero>
 <hero>
   <name>Superman</name>
   <alterego>Clark Kent</alterego>
 </hero>
 <hero>
   <name>The Flash</name>
   <alterego>Wally West</alterego>
 </hero>
</heroes>
                                       Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                               http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Node Manipulation
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$dm = $zorba->getXmlDataManager();
$dm->loadDocument('heroes.xml', file_get_contents('heroes.xml'));
$queryStr = <<< END
 delete node doc('heroes.xml')//heroes/hero[last()],
 replace node doc('heroes.xml')//heroes/hero[last()]
 with
 <hero>
       <name>The Incredible Hulk</name>
       <alterego>Bruce Banner</alterego>
 </hero>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                                           Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                                   http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Node Manipulation
(XML source, after)
<?xml version="1.0" encoding="UTF-8"?>
<heroes>
 <hero>
    <name>Spiderman</name>
    <alterego>Peter Parker</alterego>
 </hero>
 <hero>
    <name>Superman</name>
    <alterego>Clark Kent</alterego>
 </hero>
 <hero>
    <name>The Incredible Hulk</name>
    <alterego>Bruce Banner</alterego>
 </hero>
</heroes>
                                        Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                                http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
REST Parser
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<< END
  import module namespace zorba-rest =
    "http://www.zorba-xquery.com/zorba/rest-functions";
  for $post in
    zorba-rest:get("http://user:pass@api.del.icio.us/v1/posts/recent")//posts/post
  where contains($post/@tag, 'book')
  return
  <result>
    <href> {string($post/@href)} </href>
    <description> {string($post/@description)} </description>
    <tags> {string($post/@tag)} </tags>
  </result>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();

                                            Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                                    http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
REST Parser
(After)
<?xml version="1.0" encoding="UTF-8"?>

<result>

 <href>http://www.everythingphpmysql.com/</href>

 <description>How to do Everything with PHP & MySQL - Vikram Vaswani</description>

 <tags>book development php mysql web beginner</tags>

</result>

<result>

 <href>http://www.mysql-tcr.com/</href>

 <description>MySQL: The Complete Reference - Vikram Vaswani</description>

 <tags>mysql php book</tags>

</result>
JSON Parser
(JSON source)
{
    "books": [{
     "title":"Phantom Prey",
     "author":"John Sandford",
     "price":"7.99"
    }, {
     "title":"A Most Wanted Man",
     "author":"John le Carre",
     "price":"8.99"
    },{
     "title":"Beach Babylon",
     "author":"Imogen Edward-Jones",
     "price":"12.99"
    }]
}
                                       Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                               http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
JSON Parser
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<< END
 import module namespace json =
         "http://www.zorba-xquery.com/zorba/json-functions";
       let $x := json:parse('{$json}')
       for $i in $x//item
       where (xs:decimal($i/pair[@name='price']/text()) lt 10.00)
       return
       <result> {$i/pair[@name='title']/text()} </result>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                                    Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                            http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
JSON Parser
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();
$queryStr = <<< END
 import module namespace json =
         "http://www.zorba-xquery.com/zorba/json-functions";
       let $x := json:parse('{$json}')
       for $i in $x//item
       where (xs:decimal($i/pair[@name='price']/text()) lt 10.00)
       return
       <result> {$i/pair[@name='title']/text()} </result>
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();
                                    Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba"
                                                            http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
Performance
Scenario
●   Data
    ●   1 day of forecast data in XML format: 727MB
●   Workflow
    ●   Process data for a specific site
    ●   Send selected temperatures to clients
    ●   Display chart




                                    Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry"
                                http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
SimpleXML
<?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";
}
?>

                                             Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry"
                                         http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
Zorba
<?php
require_once 'Zorba/XQueryProcessor.php';
$zorba = new XQueryProcessor();


$queryStr = <<<END
  for $forecast inz: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
  <temperaturetime="{$time}" value="{$value}" />
END;
$query = $zorba->importQuery($queryStr);
echo $zorba->execute();

                                                 Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry"
                                             http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
Results

                            10000

                                      9000
                            9000

                            8000
  Memory Consumption (MB)




                            7000

                            6000

                            5000

                            4000

                            3000

                            2000

                            1000

                                                                                                      19
                               0
                                    SimpleXML                                                       Zorba
                                                   PHP Extension


                                                    Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry"
                                                http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
AWS Libraries

                10000

                9000    8589

                8000

                7000
Lines of Code




                6000

                5000
                                                                                                                              Java
                4000                                                                                                          XQuery

                                              2905
                3000
                                                                                      2309
                2000                1469
                1000                                       572                                       455
                   0
                               S3                SimpleDB                                    SNS

                                           AWS Libraries




                                                     Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry"
                                                 http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
Resources
●   Usage docs: zorba-xquery.com/html/documentation
●   Live demo: zorba-xquery.com/html/demo
●   Source code: code.launchpad.net/~zorba-
    coders/zorba/trunk
●   Modules: zorba-xquery.com/html/modules
●   Toolkit: xqdt.org
●   Forums: zorba-users [at] googlegroups [dot] com
●   News: zorba-xquery.com/html/blog
●   Twitter: @wcandillon
Questions?
Contact Information
Email:
●   vikram-vaswani.in/contact
Web:
●   www.melonfire.com
●   vikram-vaswani.in
Social networks:
●   plus.google.com/100028886433648406825
●   twitter.com/melonfire

More Related Content

PDF
Cutting Edge Data Processing with PHP & XQuery
PDF
XQuery Design Patterns
PDF
XQuery in the Cloud
PDF
Not your Grandma's XQuery
PDF
XQuery Rocks
PDF
Benchx: An XQuery benchmarking web application
ODP
Intravert Server side processing for Cassandra
PDF
kissy-past-now-future
Cutting Edge Data Processing with PHP & XQuery
XQuery Design Patterns
XQuery in the Cloud
Not your Grandma's XQuery
XQuery Rocks
Benchx: An XQuery benchmarking web application
Intravert Server side processing for Cassandra
kissy-past-now-future

What's hot (19)

PPTX
KISSY 的昨天、今天与明天
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
PDF
Datagrids with Symfony 2, Backbone and Backgrid
PDF
The state of your own hypertext preprocessor
PDF
Backbone.js: Run your Application Inside The Browser
KEY
CouchDB on Android
PDF
Testing Web Applications with GEB
PDF
HTML5 JavaScript APIs
PPTX
Introduction to node.js
PDF
Immutable Deployments with AWS CloudFormation and AWS Lambda
PDF
Fazendo mágica com ElasticSearch
KEY
PPTX
MongoDB: tips, trick and hacks
PDF
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
PDF
Introduction to Nodejs
PDF
Scala active record
PDF
Node.js in action
PDF
Remy Sharp The DOM scripting toolkit jQuery
PDF
Bottom to Top Stack Optimization - CICON2011
KISSY 的昨天、今天与明天
From mysql to MongoDB(MongoDB2011北京交流会)
Datagrids with Symfony 2, Backbone and Backgrid
The state of your own hypertext preprocessor
Backbone.js: Run your Application Inside The Browser
CouchDB on Android
Testing Web Applications with GEB
HTML5 JavaScript APIs
Introduction to node.js
Immutable Deployments with AWS CloudFormation and AWS Lambda
Fazendo mágica com ElasticSearch
MongoDB: tips, trick and hacks
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Introduction to Nodejs
Scala active record
Node.js in action
Remy Sharp The DOM scripting toolkit jQuery
Bottom to Top Stack Optimization - CICON2011
Ad

Similar to High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani (20)

KEY
Mashups with Drupal and QueryPath
PDF
Xquery basics tutorial
KEY
QueryPath, Mash-ups, and Web Services
PPT
xquery.ppt Add more information to your upload
PDF
XQuery Novelties (XML Holland 2010)
PPTX
XQuery Novelties (XML Holland 2010 - hardcore xml)
PDF
Developer & Fusion Middleware 1 | Mark Drake | An introduction to Oracle XML ...
PDF
Professional XML with PHP
PPT
PHP 5 Sucks. PHP 5 Rocks.
PDF
XML features in DB2 11 for z/OS
PDF
QueryPath: It's like PHP jQuery in Drupal!
DOC
X query
PPTX
PDF
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
PPT
XML - State of the Art
PPTX
Xpath & Xquery in XML documents for retreving data
PPTX
Xml transformation language
PPTX
Xml holland - XQuery novelties - Geert Josten
Mashups with Drupal and QueryPath
Xquery basics tutorial
QueryPath, Mash-ups, and Web Services
xquery.ppt Add more information to your upload
XQuery Novelties (XML Holland 2010)
XQuery Novelties (XML Holland 2010 - hardcore xml)
Developer & Fusion Middleware 1 | Mark Drake | An introduction to Oracle XML ...
Professional XML with PHP
PHP 5 Sucks. PHP 5 Rocks.
XML features in DB2 11 for z/OS
QueryPath: It's like PHP jQuery in Drupal!
X query
[3.3] Detection & exploitation of Xpath/Xquery Injections - Boris Savkov
XML - State of the Art
Xpath & Xquery in XML documents for retreving data
Xml transformation language
Xml holland - XQuery novelties - Geert Josten
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
Teaching material agriculture food technology
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Modernizing your data center with Dell and AMD
PDF
Empathic Computing: Creating Shared Understanding
PPTX
A Presentation on Artificial Intelligence
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The Rise and Fall of 3GPP – Time for a Sabbatical?
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Teaching material agriculture food technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Monthly Chronicles - July 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
Modernizing your data center with Dell and AMD
Empathic Computing: Creating Shared Understanding
A Presentation on Artificial Intelligence
Mobile App Security Testing_ A Comprehensive Guide.pdf
Big Data Technologies - Introduction.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...

High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani

  • 1. High Performance XQuery Processing in PHP with Zorba Vikram Vaswani November 2012
  • 2. Vikram Vaswani Founder, Melonfire ● Product design and implementation with open source technologies ● 20+ products launched for customers worldwide Author and active PHP community participant ● 7 books for McGraw-Hill USA ● 250+ tutorials for IBM dW, Zend Developer Zone and others More information at http://vikram-vaswani.in
  • 3. XML Support in PHP ● XML processing: SimpleXML and XMLReader ● XML transformation: XSLT and XML-DAS ● Event-based parsing: SAX ● Tree-based parsing: DOM ● Serialization: WDDX ● Dynamic documents: XMLWriter
  • 5. Introducing Zorba ● General purpose XQuery processor for PHP 5.3+ ● Supports XQuery v1.0 and related specifications ● Available for Windows, Linux, and Mac OS X ● Open source; Apache 2.0 License ● Project supported by FLWOR Foundation and Oracle.
  • 6. Supported XML Technologies ● XPath ● XQuery v1.0 and v3.0 ● XQuery Scripting ● Xquery Data Definition ● XPath Full-Text 1.0 ● XSLT 1.0 ● XqueryX (limited)
  • 7. PHP Installation ● Linux and MacOS X ● Libxml2: http://xmlsoft.org/ ● Iconv: http://www.gnu.org/software/libiconv/ ● libUUID: http://linux.die.net/man/3/libuuid ● ICU (C version): http://www.icu-project.org/ ● Xerces (C version): http://xerces.apache.org/xerces-c/ ● GCC and related make/cmake utilities ● Windows ● https://launchpad.net/zorba/+download ● API Wrapper ● http://zorba.s3.amazonaws.com/ZorbaPHP.zip
  • 8. PHP Installation ● Linux and MacOS X ● Libxml2: http://xmlsoft.org/ ● Iconv: http://www.gnu.org/software/libiconv/ ● libUUID: http://linux.die.net/man/3/libuuid ● ICU (C version): http://www.icu-project.org/ ● Xerces (C version): http://xerces.apache.org/xerces-c/ ● GCC and related make/cmake utilities ● Windows ● https://launchpad.net/zorba/+download ● API Wrapper ● http://zorba.s3.amazonaws.com/ZorbaPHP.zip
  • 9. Basic Usage <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); try { $queryStr = <<<END let $message := 'She sells sea shells by the sea shore' return <result>{$message}</result> END; $query = $zorba->importQuery($queryStr); $result = $zorba->execute(); echo $result; } catch (Exception $e) { die('ERROR:' . $e->getMessage()); } Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 12. Arithmetic Operators <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<<END declare variable $x := 30; declare variable $y := 12; let $r := $x + $y return <result> {$r} </result> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 13. Sequences <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<<END let $dolls := ('Echo', 'Victor', 'Sierra', 'November', 'Alpha') return <results> { for $doll in $dolls return <name>{$doll}</name> } </results> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 14. String Functions <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<<END let $dolls := ('Echo', 'Victor', 'Sierra', 'November', 'Alpha') return <results> {string-join($dolls, ', ')} </results> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 15. Math Functions <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<<END let $set := (1 to 20) return <results> { for $i in $set where ($i mod 2 eq 0) return <value> {$i} </value> } </results> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 16. Data Filtering (XML source) <?xml version="1.0"?> <data> <record> <code>ABW</code> <name>Aruba</name> <continent>North America</continent> <year></year> <population>103000</population> <gnp>828.00</gnp> <capital> <name>Oranjestad</name> <population>29034</population> </capital> </record> ... </data> Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 17. Data Filtering (Simple) <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $dm = $zorba->getXmlDataManager(); $dm->loadDocument('data.xml', file_get_contents('data.xml')); $queryStr = <<< END for $record in doc('data.xml')//record let $name := $record/name/text() let $code := $record/code/text() return <name code="{$code}">{$name}</name> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 18. Data Filtering (Complex) <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); dm = $zorba->getXmlDataManager(); $dm->loadDocument('data.xml', file_get_contents('data.xml')); $queryStr = <<< END for $record in doc('data.xml')//record let $name := $record/name/text() let $code := $record/code/text() let $population := $record/population/text() where contains($record/continent/text(), 'Europe') where (xs:integer($population) gt 1000000) order by $population descending return <name code="{$code}" population="{$population}">{$name}</name> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 19. Node Manipulation (XML source, before) <?xml version='1.0'?> <heroes> <hero> <name>Spiderman</name> <alterego>Peter Parker</alterego> </hero> <hero> <name>Superman</name> <alterego>Clark Kent</alterego> </hero> <hero> <name>The Flash</name> <alterego>Wally West</alterego> </hero> </heroes> Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 20. Node Manipulation <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $dm = $zorba->getXmlDataManager(); $dm->loadDocument('heroes.xml', file_get_contents('heroes.xml')); $queryStr = <<< END delete node doc('heroes.xml')//heroes/hero[last()], replace node doc('heroes.xml')//heroes/hero[last()] with <hero> <name>The Incredible Hulk</name> <alterego>Bruce Banner</alterego> </hero> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 21. Node Manipulation (XML source, after) <?xml version="1.0" encoding="UTF-8"?> <heroes> <hero> <name>Spiderman</name> <alterego>Peter Parker</alterego> </hero> <hero> <name>Superman</name> <alterego>Clark Kent</alterego> </hero> <hero> <name>The Incredible Hulk</name> <alterego>Bruce Banner</alterego> </hero> </heroes> Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 22. REST Parser <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<< END import module namespace zorba-rest = "http://www.zorba-xquery.com/zorba/rest-functions"; for $post in zorba-rest:get("http://user:pass@api.del.icio.us/v1/posts/recent")//posts/post where contains($post/@tag, 'book') return <result> <href> {string($post/@href)} </href> <description> {string($post/@description)} </description> <tags> {string($post/@tag)} </tags> </result> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 23. REST Parser (After) <?xml version="1.0" encoding="UTF-8"?> <result> <href>http://www.everythingphpmysql.com/</href> <description>How to do Everything with PHP & MySQL - Vikram Vaswani</description> <tags>book development php mysql web beginner</tags> </result> <result> <href>http://www.mysql-tcr.com/</href> <description>MySQL: The Complete Reference - Vikram Vaswani</description> <tags>mysql php book</tags> </result>
  • 24. JSON Parser (JSON source) { "books": [{ "title":"Phantom Prey", "author":"John Sandford", "price":"7.99" }, { "title":"A Most Wanted Man", "author":"John le Carre", "price":"8.99" },{ "title":"Beach Babylon", "author":"Imogen Edward-Jones", "price":"12.99" }] } Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 25. JSON Parser <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<< END import module namespace json = "http://www.zorba-xquery.com/zorba/json-functions"; let $x := json:parse('{$json}') for $i in $x//item where (xs:decimal($i/pair[@name='price']/text()) lt 10.00) return <result> {$i/pair[@name='title']/text()} </result> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 26. JSON Parser <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<< END import module namespace json = "http://www.zorba-xquery.com/zorba/json-functions"; let $x := json:parse('{$json}') for $i in $x//item where (xs:decimal($i/pair[@name='price']/text()) lt 10.00) return <result> {$i/pair[@name='title']/text()} </result> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: Vikram Vaswani, " Building XQue ry-po we re d applicatio ns with PHP and Zo rba" http: //www. ibm. co m/de ve lo pe rwo rks/library/x-zo rba/inde x. html
  • 28. Scenario ● Data ● 1 day of forecast data in XML format: 727MB ● Workflow ● Process data for a specific site ● Send selected temperatures to clients ● Display chart Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry" http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
  • 29. SimpleXML <?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"; } ?> Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry" http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
  • 30. Zorba <?php require_once 'Zorba/XQueryProcessor.php'; $zorba = new XQueryProcessor(); $queryStr = <<<END for $forecast inz: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 <temperaturetime="{$time}" value="{$value}" /> END; $query = $zorba->importQuery($queryStr); echo $zorba->execute(); Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry" http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
  • 31. Results 10000 9000 9000 8000 Memory Consumption (MB) 7000 6000 5000 4000 3000 2000 1000 19 0 SimpleXML Zorba PHP Extension Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry" http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
  • 32. AWS Libraries 10000 9000 8589 8000 7000 Lines of Code 6000 5000 Java 4000 XQuery 2905 3000 2309 2000 1469 1000 572 455 0 S3 SimpleDB SNS AWS Libraries Cre dit: W illiam Candillo n, " Cutting Edg e Data Pro ce ssing with PHP & XQue ry" http: //www. slide share . ne t/wcandillo n/cutting -e dg e -data-pro ce ssing -with-php-xque ry
  • 33. Resources ● Usage docs: zorba-xquery.com/html/documentation ● Live demo: zorba-xquery.com/html/demo ● Source code: code.launchpad.net/~zorba- coders/zorba/trunk ● Modules: zorba-xquery.com/html/modules ● Toolkit: xqdt.org ● Forums: zorba-users [at] googlegroups [dot] com ● News: zorba-xquery.com/html/blog ● Twitter: @wcandillon
  • 35. Contact Information Email: ● vikram-vaswani.in/contact Web: ● www.melonfire.com ● vikram-vaswani.in Social networks: ● plus.google.com/100028886433648406825 ● twitter.com/melonfire