使用 PHP 执行 XQuery

发布于 2024-08-20 15:08:43 字数 108 浏览 4 评论 0 原文

如何在 XQuery ="noreferrer">PHP?你能举个例子吗?

谢谢。

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浅唱々樱花落 2024-08-27 15:08:43

PHP 没有任何支持 XQuery 的本机或通用 XML 解析器(如果我错了,请有人告诉我)。然而,它确实有两个非常标准的扩展来处理 XPath 查询。

我个人认为 simplexml 是两者中更好的一个。您只需使用:

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

然后循环结果。

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

祝你好运。

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.

煮酒 2024-08-27 15:08:43

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

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

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

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

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);
梦在夏天 2024-08-27 15:08:43

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

BaseX 客户端

include("BaseXClient.php");

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

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();
孤独患者 2024-08-27 15:08:43

也可以与 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 />';
}

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 />';
}
蘸点软妹酱 2024-08-27 15:08:43

http://phpxmlclasses.sourceforge.net/ 上有一个页面,其中包含一个 XQuery Lite 类:

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

我从未使用过它,也不知道它的性能如何。

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.

内心荒芜 2024-08-27 15:08:43

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

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(); 
?>
云巢 2024-08-27 15:08:43

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文