代码之家  ›  专栏  ›  技术社区  ›  abernier

用PHP执行XQuery

  •  10
  • abernier  · 技术社区  · 16 年前

    如何执行 XQuery 在里面 PHP ? 你能给我举个例子吗?

    8 回复  |  直到 16 年前
        1
  •  6
  •   Community Mohan Dere    14 年前

    梨包装: 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);
    
        2
  •  5
  •   Anthony    16 年前

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

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

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

    祝你好运。

        3
  •  2
  •   Gordon Haim Evgi    15 年前

    $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
  •  2
  •   lacmuch    11 年前

    使用BaseX。它稳定,可伸缩,快速! (但需要服务器才能运行)

    BaseX clients

    include("BaseXClient.php");
    
    $session = new Session("localhost", 1984, "admin", "admin");
    print $session->execute("xquery 1 to 10");
    $session->close();
    
        5
  •  1
  •   Gordon Haim Evgi    15 年前

    这一页在 http://phpxmlclasses.sourceforge.net/ 它有一个XQueryLite类:

    我从来没有用过它,也不知道它的性能如何。

        6
  •  1
  •   wcandillon    14 年前

    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(); 
    ?>
    
        7
  •  1
  •   wcandillon    14 年前

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

        8
  •  -1
  •   Dominik    16 年前
    推荐文章