梨包: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);
Run Code Online (Sandbox Code Playgroud)
PHP没有任何支持XQuery的本机或通用XML解析器(如果我错了,有人让我知道).但它有两个非常标准的扩展来处理XPath查询.
我个人认为simplexml这两者更好.你只需使用:
$xml = new simplexml($some_xml_string);
$xpath_results = $xml -> Xpath("//a/b");
Run Code Online (Sandbox Code Playgroud)
然后循环结果.
广泛的DOM类也支持Xpath查询.就我看来,使用DOM的唯一真正优势是可以直接从较大的XML对象中修改或删除结果.
祝好运.