This is a simple example that demonstrate how to use the Zorba XQuery Engine to create, compile, and execute queries.
16 sys.path.insert(0,
'/builddir/build/BUILD/zorba-2.9.1/build/swig/python')
20 xquery = zorba.compileQuery(
"1+2")
21 print xquery.printPlanAsXML()
22 print xquery.execute()
26 xquery = zorba.compileQuery(
"(1,2,3,4,5)")
27 iter = xquery.iterator()
29 item = zorba_api.Item_createEmptyItem()
30 while iter.next(item):
31 print item.getStringValue()
38 xquery = zorba.compileQuery(
"1 div 0")
39 print xquery.execute()
40 except RuntimeError, e:
46 xquery = zorba.compileQuery(
"for $i in (1,2,")
47 print xquery.execute()
48 except RuntimeError, e:
53 dataManager = zorba.getXmlDataManager()
54 docIter = dataManager.parseXML(
"<books><book>Book 1</book><book>Book 2</book></books>")
57 doc = zorba_api.Item_createEmptyItem()
63 docManager = dataManager.getDocumentManager()
64 docManager.put(
"books.xml", doc)
65 xquery = zorba.compileQuery(
"doc('books.xml')//book")
66 print xquery.execute()
67 docManager.remove(
"books.xml");
71 dataManager = zorba.getXmlDataManager()
72 docIter = dataManager.parseXML(
"<books><book>Book 1</book><book>Book 2</book></books>")
75 doc = zorba_api.Item_createEmptyItem()
81 xquery = zorba.compileQuery(
".")
82 dynCtx = xquery.getDynamicContext();
84 dynCtx.setContextItem(doc);
85 print xquery.execute()
88 def example7(zorba, file):
90 print 'Executing: '+file+
'.xq'
91 f = open(file+
'.xq',
'r')
94 xquery = zorba.compileQuery(lines)
95 result = xquery.execute()
97 f = open(file +
'_result.xml',
'w')
104 xquery = zorba.compileQuery(
'(xs:QName (" fn:a ") eq QName ("http://www.w3.org/2005/xpath-functions", "pre:a")) and (xs:QName (" x ") eq xs:QName ("x"))')
105 print xquery.execute()
106 except RuntimeError, e:
110 store = zorba_api.InMemoryStore_getInstance()
111 zorba = zorba_api.Zorba_getInstance(store)
140 example7(zorba,
'test'+str(x))
148 zorba_api.InMemoryStore_shutdown(store)