python_test.py

This is a simple example that demonstrate how to use the Zorba XQuery Engine to create, compile, and execute queries.

1 # Copyright 2006-2008 The FLWOR Foundation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import sys
16 sys.path.insert(0, '/builddir/build/BUILD/zorba-2.9.1/build/swig/python')
17 import zorba_api
18 
19 def example1(zorba):
20  xquery = zorba.compileQuery("1+2")
21  print xquery.printPlanAsXML()
22  print xquery.execute()
23  return
24 
25 def example2(zorba):
26  xquery = zorba.compileQuery("(1,2,3,4,5)")
27  iter = xquery.iterator()
28  iter.open()
29  item = zorba_api.Item_createEmptyItem()
30  while iter.next(item):
31  print item.getStringValue()
32  iter.close()
33  iter.destroy()
34  return
35 
36 def example3(zorba):
37  try:
38  xquery = zorba.compileQuery("1 div 0")
39  print xquery.execute()
40  except RuntimeError, e:
41  print e
42  return
43 
44 def example4(zorba):
45  try:
46  xquery = zorba.compileQuery("for $i in (1,2,")
47  print xquery.execute()
48  except RuntimeError, e:
49  print e
50  return
51 
52 def example5(zorba):
53  dataManager = zorba.getXmlDataManager()
54  docIter = dataManager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>")
55  docIter.open();
56 
57  doc = zorba_api.Item_createEmptyItem()
58  docIter.next(doc)
59 
60  docIter.close()
61  docIter.destroy()
62 
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");
68  return
69 
70 def example6(zorba):
71  dataManager = zorba.getXmlDataManager()
72  docIter = dataManager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>")
73  docIter.open();
74 
75  doc = zorba_api.Item_createEmptyItem()
76  docIter.next(doc)
77 
78  docIter.close()
79  docIter.destroy()
80 
81  xquery = zorba.compileQuery(".")
82  dynCtx = xquery.getDynamicContext();
83 
84  dynCtx.setContextItem(doc);
85  print xquery.execute()
86  return
87 
88 def example7(zorba, file):
89  #Read and write result
90  print 'Executing: '+file+'.xq'
91  f = open(file+'.xq', 'r')
92  lines = f.read()
93  f.close()
94  xquery = zorba.compileQuery(lines)
95  result = xquery.execute()
96  print result
97  f = open(file + '_result.xml', 'w')
98  f.write(result)
99  f.close()
100  return
101 
102 def example8(zorba):
103  try:
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:
107  print e
108  return
109 
110 store = zorba_api.InMemoryStore_getInstance()
111 zorba = zorba_api.Zorba_getInstance(store)
112 
113 print "Example1:"
114 example1(zorba)
115 print ""
116 
117 print "Example2:"
118 example2(zorba)
119 print ""
120 
121 print "Example3:"
122 example3(zorba)
123 print ""
124 
125 print "Example4:"
126 example4(zorba)
127 print ""
128 
129 print "Example5:"
130 example5(zorba)
131 print ""
132 
133 print "Example6:"
134 example6(zorba)
135 print ""
136 
137 print "Example7:"
138 a = range(10)
139 for x in a:
140  example7(zorba, 'test'+str(x))
141 print ""
142 
143 print "Example8:"
144 example8(zorba)
145 print ""
146 
147 zorba.shutdown()
148 zorba_api.InMemoryStore_shutdown(store)
149