zorba3.0的編譯安裝與python binding的使用

自己的項目必須用一個全功能的xquery引擎,並要在python中調用,找了很久只找到了zorba,但是xquery本身非常冷門,zorba也是找不到用武之地,活躍度非常小,導致文檔的結構與排版非常奇葩而且文檔和源碼有兩處錯誤,再加上zorba本身需要很多的第三方包支持,我第一次嘗試編譯zorba的時候基本毫無頭緒。但是沒有辦法,市面上的xquery就它一個,非他不可,所以只能頭硬死磕,終於在解決所有依賴又找到兩處錯誤後成功安裝了zorba。現在分享出來,如果你也要安zorba,這篇文章能給你省下幾天的時間。


1.安裝

依賴的源碼包都是在官網下載,版本不都是最新的,沒有測試過任何依賴版本兼容問題,只知道這一套依賴可以成功。下面是在ubuntu 12上的僞命令,因爲做總結時已經是第二次安裝了,依賴包是拷過來的,所以沒有寫完整的url,另外系統可能也比較老了,請見諒。

sudo apg-get install cmake

wget libiconv-1.14.tar.gz
tar -zxvf libiconv-1.14.tar.gz
./configure
make
sudo make install


wget libuuid-1.0.3.tar.gz
tar -zxvf libuuid-1.0.3.tar.gz
./configure
make
sudo make install


wget libxml2-2.7.2.tar.gz
tar -zxvf libxml2-2.7.2.tar.gz
./configure
make
sudo make install


wget libxslt-git-snapshot.tar.gz
tar -zxvf libxslt-git-snapshot.tar.gz
./configure
make
sudo make install


wget swig-2.0.4.tar.gz
tar -zxvf swig-2.0.4.tar.gz
./configure
make
sudo make install


wget tidy-5.2.0-64bit.deb
sudo dpkg -i tidy-5.2.0-64bit.deb


wget xerces-c-3.1.4.zip
unzip -q xerces-c-3.1.4.zip
./configure
make
sudo make install


wget zorba-3.0.tar.gz
tar -zxvf zorba-3.0.tar.gz
cd zorba-3.0/
mkdir build
cd build/
cmake -D ZORBA_SUPPRESS_SWIG=OFF ..   #這裏官網文檔上標註的是-D ZORBA_USE_SWIG=ON查了cmake配置才發現變量名已經改了,而且意義也相反
make
vim {zorba_build_dir}/swig/python/cmake_install.cmake  #第40行,將{...}/build/swig/python/_zorba_api_python.so改爲{...}/build/swig/python/_zorba_api.so
sudo make install


經過上面的步驟zorba就可以使用了,在命令行上打zorba 就會看到相應的提示

$ zorba
Error: No queries submitted 
Usage: zorba -q '1 + 1' execute an inline query 
 zorba file.xq execute a query from a file 
 Use -h for help.


然後進入到{zorba_build_dir}/swig/python/目錄下,在進入python交互界面,就可以引用zorba的python模塊了:

$ cd {zorba_build_dir}/swig/python/
$ python
>>>import zorba_api
>>>

2.python binding的使用

test.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Aug 7, 2016


@author: finetu
'''
import zorba_api
from bs4 import BeautifulSoup


soup = BeautifulSoup(open("index2.html"),"lxml")
# print str(soup.prettify())
all_the_text = soup.prettify().encode("utf8")


store = zorba_api.InMemoryStore_getInstance()
zorba = zorba_api.Zorba_getInstance(store)


dataManager = zorba.getXmlDataManager()
docIter = dataManager.parseXML(all_the_text)
docIter.open();


doc = zorba_api.Item_createEmptyItem()
docIter.next(doc)
docIter.close()
docIter.destroy()


docManager = dataManager.getDocumentManager()
docManager.put("response", doc)


xquery = zorba.compileQuery(
"""
for $_postDiv in doc('response')//div[@class='post f list-post']
    let $_postTitle := {data($_postDiv/div[@class='indexs']/h2/a)},
        $_postHref := {data($_postDiv/div[@class='indexs']/h2/a/@href)},
        $_postAuthor := {data($_postDiv/div[@class='indexs']/div[@class='time_s']/a)}
    return 
        <item>
        <title>{$_postTitle}</title>
        <href>{$_postHref}</href>
        <author>{$_postAuthor}</author>
        </item>
""")
dynCtx = xquery.getDynamicContext();


dynCtx.setContextItem(doc);
print xquery.execute()


index2.html

    <div id="content"> 
   <h3 class="title"><a href="http://jandan.net/2016/09/02" title="2016年9月02日 ">今天</a> &middot; <a href="http://jandan.net/2016/09/01">昨天</a> </h3> 
   <div class="post f list-post"> 
    <div class="thumbs_b">
     <a href="http://jandan.net/2016/09/01/temporal-discounting.html" target="_blank"><img class="lazy" data-original="//tankr.net/s/custom/STVK.jpg" width="175" height="98" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" /></a>
    </div> 
    <div class="indexs"> 
     <span class="comment-link" title="20 hours ago">24</span> 
     <h2><a href="http://jandan.net/2016/09/01/temporal-discounting.html" target="_blank">讓你變窮的「時間貼現」</a></h2> 
     <div class="time_s">
      <a href="http://jandan.net/author/Miriam">蛋奶</a> &middot; 
      <strong><a href="http://jandan.net/tag/%e5%b0%8f%e8%b4%b4%e5%a3%ab" rel="tag">小貼士</a></strong> 
     </div> 相比起明天才能看到的錢,人們更享受今天花錢帶來的快感。 
     <a id="jandan-zan-81990" class="jandan-zan" onclick="jandan_zan(81990)" href="javascript:;"> <span class="zan-icon">⊙8</span> </a> 
    </div> 
   </div> 
   <div class="post f list-post"> 
    <div class="thumbs_b">
     <a href="http://jandan.net/2016/09/01/egyptians-strategy.html" target="_blank"><img class="lazy" data-original="//tankr.net/s/custom/EHJE.jpg" width="175" height="98" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" /></a>
    </div> 
    <div class="indexs"> 
     <span class="comment-link" title="21 hours ago">51</span> 
     <h2><a href="http://jandan.net/2016/09/01/egyptians-strategy.html" target="_blank">古埃及人告訴你如何應對強詞奪理的上司</a></h2> 
     <div class="time_s">
      <a href="http://jandan.net/author/Miriam">蛋奶</a> &middot; 
      <strong><a href="http://jandan.net/tag/%e8%80%83%e5%8f%a4" rel="tag">考古</a></strong> 
     </div> 4000年的古埃及箴言仍不過時。 
     <a id="jandan-zan-81983" class="jandan-zan" onclick="jandan_zan(81983)" href="javascript:;"> <span class="zan-icon">⊙20</span> </a> 
    </div> 
   </div> 
  </div>
運行test.py

$ python test.py
<?xml version="1.0" encoding="UTF-8"?>
<item><title>
       讓你變窮的「時間貼現」
      </title><href>http://jandan.net/2016/09/01/temporal-discounting.html</href><author>
       蛋奶
      </author></item><item><title>
       古埃及人告訴你如何應對強詞奪理的上司
      </title><href>http://jandan.net/2016/09/01/egyptians-strategy.html</href><author>
       蛋奶
      </author></item>

python 模塊的使用也非常蹩腳,可能是沒有領會到精髓,貌似zorba必須從一個docManager 對象裏面讀取內容,重複使用時要注意把docmanager清空。但是無論如何它是個全功能的xquery引擎,和JSONiq引擎,對於需要這種工具的開發者來說都是心懷感激。

轉載請註明出處:http://blog.csdn.net/gklifg/article/details/54288555

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章