使用 PHP 执行 XQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2211743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a XQuery with PHP
提问by abernier
回答by netricate
pear package: http://www.pecl.php.net/package/Zorba(error 404 link)
梨包:http: //www.pecl.php.net/package/Zorba(错误404链接)
NEW (2011): http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery
新(2011):http: //www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery
zorba documentation: http://www.zorba-xquery.com/
zorba 文档:http://www.zorba-xquery.com/
zorba docs provide a simple example:
zorba 文档提供了一个简单的例子:
//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);
回答by Anthony
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.
PHP 没有任何支持 XQuery 的本机或通用 XML 解析器(如果我错了,有人告诉我)。然而,它确实有两个非常标准的扩展来处理 XPath 查询。
I personally think simplexmlis the better of the two. You would simply use:
个人认为simplexml两者比较好。您只需使用:
$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.
广泛的 DOM 类也支持 Xpath 查询。在我看来,使用 DOM 的唯一真正优势是可以直接从较大的 XML 对象中修改或删除结果。
Good luck.
祝你好运。
回答by lacmuch
Use BaseX. Its stable, scaleable, and fast! (but you need a server to run)
使用 BaseX。它稳定、可扩展且快速!(但你需要一个服务器来运行)
include("BaseXClient.php");
$session = new Session("localhost", 1984, "admin", "admin");
print $session->execute("xquery 1 to 10");
$session->close();
回答by user267599
its also posible with DOMDocument and DOMXPath
它也可以用于 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 />';
}
回答by Gordon
There is this page at http://phpxmlclasses.sourceforge.net/that has an XQuery Lite class:
http://phpxmlclasses.sourceforge.net/上的这个页面有一个 XQuery Lite 类:
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.
Xquery Lite 1.0 语言的 PHP 实现,一种基于 Xquery 1.0 查询 XML 文档的语言 该类基于 DOM 扩展,允许对文件、php 字符串或组合中的 XML 文档执行 Xquery Lite 查询。
I have never used it and do not know how it performs.
我从未使用过它,不知道它的性能如何。
回答by wcandillon
The following link should be useful: http://dl.dropbox.com/u/1487285/php/php.html
以下链接应该有用: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();
?>
回答by wcandillon
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.
对于共享托管场景,我建议使用类似 28msec (http://www.28msec.com) 的东西,它使您能够基于 Zorba XQuery 处理器构建 RESTful 服务。从 PHP,您可以通过 REST 连接到 28 毫秒。
回答by Dominik
Did you have a look at http://www.pecl.php.net/package/Zorba?

