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.
//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);
//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);
$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 />';
}
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.
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.
发布评论
评论(8)
PHP 没有任何支持 XQuery 的本机或通用 XML 解析器(如果我错了,请有人告诉我)。然而,它确实有两个非常标准的扩展来处理 XPath 查询。
我个人认为
simplexml
是两者中更好的一个。您只需使用:然后循环结果。
广泛的 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: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.
梨包: 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 文档提供了一个简单的示例:
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:
使用 BaseX。它稳定、可扩展且快速!
(但你需要一个服务器来运行)
BaseX 客户端
Use BaseX. Its stable, scaleable, and fast!
(but you need a server to run)
BaseX clients
也可以与 DOMDocument 和 DOMXPath 一起使用
its also posible with DOMDocument and DOMXPath
http://phpxmlclasses.sourceforge.net/ 上有一个页面,其中包含一个 XQuery Lite 类:
我从未使用过它,也不知道它的性能如何。
There is this page at http://phpxmlclasses.sourceforge.net/ that has an XQuery Lite class:
I have never used it and do not know how it performs.
以下链接应该很有用: http://dl.dropbox.com/u/ 1487285/php/php.html
The following link should be useful: http://dl.dropbox.com/u/1487285/php/php.html
对于共享托管场景,我建议使用 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.
您是否看过 http://www.pecl.php.net/package/Zorba< /a>?
Did you have a look at http://www.pecl.php.net/package/Zorba ?