Execute a XQuery with PHP
Asked Answered
A

8

13

How to execute a XQuery in PHP? Can you give me an example?

Thank you.

Atrocious answered 6/2, 2010 at 2:23 Comment(0)
D
6

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);
Difficult answered 6/2, 2010 at 2:43 Comment(2)
Same comment as for Dominik (#2212243). Really, nothing more simple?Atrocious
That package no longer exists in PECLElectric
V
6

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.

Varve answered 6/2, 2010 at 2:44 Comment(5)
XPath isn't the same as XQuery.Ammonite
I know, right? That's probably why the first thing I said was that PHP doesn't have native support for XQUERY. I guess next time, I shouldn't add helpful alternative solutions.Varve
I still don't think that for a question as generic as this one xpath is a viable alternative for xquery. At least it requires some big assumptions. But anyway....Ammonite
Fair enough. Basically pointing to XPath is really just to spare them the journey I've taken with PHP and XML, where I read about some XML standard technology, get excited, find out it's kind of supported in PHP, and even then not without recompiling. Happend with me for XQuery, XSLT, SOAP/WSDL, XPath, and I'm sure it will happen again. Not hating on PHP, just saying XML will break hearts, with all of it's big promises.Varve
On that I can full-heartedly agree. "XML is like violence. If it doesn't solve your problems you didn't use enough of it". On the one hand it's abused too often, on the other hand it's too often used with sub-par tools. And strangely enough both cases coincide way, way too often within the same project (or company). | And looking up the subsequent questions of abernier it seems as xpath did the trick.Ammonite
E
3

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();
Enameling answered 19/3, 2015 at 17:27 Comment(0)
G
2

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 />';
}
Galliot answered 6/2, 2010 at 7:4 Comment(0)
E
1

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

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.

Electric answered 22/1, 2011 at 18:57 Comment(0)
C
1

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(); 
?>
Cab answered 25/12, 2011 at 15:23 Comment(0)
C
1

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.

Cab answered 29/2, 2012 at 8:32 Comment(0)
S
-1

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

Soper answered 6/2, 2010 at 2:42 Comment(2)
Dude! Compiling just for executing a simple query?! Sounds like rocket science here... nothing more simple?Atrocious
That package no longer exists in PECLElectric

© 2022 - 2024 — McMap. All rights reserved.