处理使用Python进行的eXist-db查询的结果

时间:2013-08-14 12:22:18

标签: python xquery exist-db

我在python中完成了以下代码,以获取存储在eXist-db中的XML查询响应。我得到了值,但问题是在下面的输出中给出的'instant'类型。这是我的代码:

from eulexistdb import db
class TestExist:
    def __init__(self):
        self.db = db.ExistDB("http://localhost:8899/exist/")   

    def get_res(self,query):
        #result = list()
        res = self.db.executeQuery(query)
        hits = self.db.getHits(res)        
        for i in range(hits):
            print self.db.retrieve(res,i)
            print type(self.db.retrieve(res,i))
xquery = '''
let $x:= doc("/db/sample/books.xml")
return $x/bookstore/book/price/text()'''
a = TestExist()
a.get_res(xquery)

现在查询工作正常,结果也打印为:

30.00
<type 'instance'>
29.99
<type 'instance'>
49.99
<type 'instance'>
39.95
<type 'instance'>

我想要的是返回列表'result'中附加的值。我尝试了类型转换,但失败了。我如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

这很奇怪 - docs似乎说db.retrieve函数应该返回一个字符串,但显然它没有。在任何情况下,由于print语句从中获取了一个有用的字符串,其中一个应该可以工作:

result.append(str(self.db.retrieve(res,i)))

result.append(repr(self.db.retrieve(res,i)))

取消注释#result = list()行,并将以上其中一项添加到for循环中。