开发者

Execute a XQuery with PHP

How to execute a XQuery in PHP? Can开发者_高级运维 you give me an example?

Thank you.


PHP does not have any native or common XML parsers that support XQuery (If I'm wrong, someone let me know). It does however have two pretty standard extensions that handle XPath queries.

I personally think simplexml is the better of the two. You would simply use:

$xml = new simplexml($some_xml_string);
$xpath_results = $xml -> Xpath("//a/b");

And then loop through the results.

The extensive DOM class supports Xpath queries as well. The only real advantage, as far as I see it, to using DOM is that the results can be modified or deleted straight out of the larger XML object.

Good luck.


pear package: http://www.pecl.php.net/package/Zorba (error 404 link)

NEW (2011): http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery

zorba documentation: http://www.zorba-xquery.com/

zorba docs provide a simple example:

//Include for the Object-Oriented API
require ‘zorba_api.php’;

//Initialization of Zorba store
$store = InMemoryStore::getInstance();
//Initialization of Zorba
$zorba = Zorba::getInstance($store);

$xquery = <<< EOT
let $message := ‘Hello World!’
return
<results>
   <message>{$message}</message>
</results>
EOT;

//Compile the query
$lQuery = $zorba->compileQuery($xquery);
//Run the query…
echo $lQuery->execute();
//…and destroy it
$lQuery->destroy();

//Shutdown of Zorba
$zorba->shutdown();
//Shutdown of Zorba store
InMemoryStore::shutdown($store);


Use BaseX. Its stable, scaleable, and fast! (but you need a server to run)

BaseX clients

include("BaseXClient.php");

$session = new Session("localhost", 1984, "admin", "admin");
print $session->execute("xquery 1 to 10");
$session->close();


its also posible with DOMDocument and DOMXPath

$doc = new DOMDocument();
$doc->load('http://www.example.com/some.xml');
$xpd = new DOMXPath($doc);
false&&$node = new DOMElement();//this is for my IDE to have intellysense

$result = $xpd->query('//a/b');
foreach($result as $node){
    echo $node->nodeName.'<br />';
}


There is this page at http://phpxmlclasses.sourceforge.net/ that has an XQuery Lite class:

  • http://phpxmlclasses.sourceforge.net/show_doc.php?class=class_xquery_lite.html

A PHP implementation of the Xquery Lite 1.0 language, a language to query XML documents based on Xquery 1.0 This class is based on the DOM extension and allows to execute Xquery Lite queries for XML documents in files, php strings or combinations.

I have never used it and do not know how it performs.


The following link should be useful: http://dl.dropbox.com/u/1487285/php/php.html

<?php
require_once 'Zorba/XQueryProcessor.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(); 
?>


For shared hosting scenarios, I suggest to use something like 28msec (http://www.28msec.com) which enables you to build RESTful services based on top of the Zorba XQuery processor. From PHP you can connect to 28msec via REST.


Did you have a look at http://www.pecl.php.net/package/Zorba ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜