This is a simple example that demonstrate how to use the Zorba XQuery Engine to create, compile, and execute queries.
<?php /* * Copyright 2006-2012 The FLWOR Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require '/builddir/build/BUILD/zorba-2.2.0/build/swig/php/Zorba/zorba_api_wrapper.php'; function example_1(Zorba $aZorba) { $lQuery = $aZorba->compileQuery('1+1'); echo $lQuery->execute(); $lQuery->destroy(); return true; } function example_2(Zorba $aZorba) { $lQuery = $aZorba->compileQuery('1+1'); $lIterator = $lQuery->iterator(); $lIterator->open(); $lItem = Item::createEmptyItem(); while($lIterator->next($lItem)) { echo $lItem->getStringValue() . "\n"; } $lIterator->close(); $lIterator->destroy(); $lQuery->destroy(); return true; } function example_3(Zorba $aZorba) { $lQuery = $aZorba->compileQuery('1 div 0'); try { echo $lQuery->execute(); } catch ( Exception $e ) { echo $e->getMessage() . "\n"; $lQuery->destroy(); return true; } $lQuery->destroy(); return false; } function example_4(Zorba $aZorba) { try { $lQuery = $aZorba->compileQuery('for $i in (1,2,3)'); } catch ( Exception $e ) { echo $e->getMessage() . "\n"; return true; } return false; } function example_5(Zorba $aZorba) { $lDataManager = $aZorba->getXmlDataManager(); $lDocMgr = $lDataManager->getDocumentManager(); $iter = $lDataManager->parseXML("<books><book>Book 1</book><book>Book 2</book></books>"); $iter->open(); $doc = Item::createEmptyItem(); $iter->next($doc); $iter->close(); $iter->destroy(); $lDocMgr->put("books.xml", $doc); $lQuery = $aZorba->compileQuery("doc('books.xml')//book"); echo $lQuery->execute(); $lQuery->destroy(); return true; } $store = InMemoryStore::getInstance(); $zorba = Zorba::getInstance($store); print "Example1:\n"; $res = example_1($zorba); assert($res); print "\n\n"; print "Example2:\n"; $res = example_2($zorba); assert($res); print "\n\n"; print "Example3:\n"; $res = example_3($zorba); assert($res); print "\n\n"; print "Example4:\n"; $res = example_4($zorba); assert($res); print "\n\n"; print "Example5:\n"; $res = example_5($zorba); assert($res); print "\n\n"; $zorba->shutdown(); InMemoryStore::shutdown($store); ?>