用PHP执行XQuery

时间:2010-02-06 02:23:17

标签: php xquery

如何在XQuery中执行PHP?你能举个例子吗?

谢谢。

8 个答案:

答案 0 :(得分:6)

梨包:http://www.pecl.php.net/package/Zorba(错误404链接)

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

zorba文档:http://www.zorba-xquery.com/

zorba docs提供了一个简单的例子:

//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);

答案 1 :(得分:5)

PHP没有支持XQuery的任何本机或通用XML解析器(如果我错了,有人让我知道)。但它有两个非常标准的扩展来处理XPath查询。

我个人认为simplexml是两者中最好的。你只需使用:

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

然后循环结果。

广泛的DOM类也支持Xpath查询。就我看来,使用DOM的唯一真正优势是可以直接从较大的XML对象中修改或删除结果。

祝你好运。

答案 2 :(得分:2)

使用BaseX。它稳定,可扩展,快速! (但你需要一台服务器来运行)

BaseX clients

include("BaseXClient.php");

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

答案 3 :(得分:1)

它还具有DOMDocument和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 />';
}

答案 4 :(得分:1)

http://phpxmlclasses.sourceforge.net/的这个页面有一个XQuery Lite类:

  

Xquery Lite 1.0语言的PHP实现,这是一种基于Xquery 1.0查询XML文档的语言。该类基于DOM扩展,允许对文件,php字符串或组合中的XML文档执行Xquery Lite查询。

我从未使用它,也不知道它是如何表现的。

答案 5 :(得分:1)

以下链接应该有用: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(); 
?>

答案 6 :(得分:1)

对于共享主机方案,我建议使用28msec(http://www.28msec.com)之类的东西,它使您能够基于Zorba XQuery处理器构建RESTful服务。 从PHP,您可以通过REST连接到28毫秒。

答案 7 :(得分:-1)