22

Is there any existing way to run XQuery under python? (not starting to build a parser yourself in other words).

I got a ton of legacy XQuery that I want to port to our new system, or rather I want to port the framework and not XQuery.

Therefore: Is there any library that allows me to run XQuery under python?

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
Ooki
  • 351
  • 1
  • 2
  • 6

4 Answers4

14

Sort of ...

Looking through the W3C implementations list for XQuery there is:

  1. Python bindings for Zorba
  2. Sedna is a free native XML database with API for Python.

A few Python examples with Zorba, from here

import sys
import zorba_api

def example1(zorba):
  xquery = zorba.compileQuery("1+2")
  print xquery.printPlanAsXML()
  print xquery.execute()
  return

def example2(zorba):
  xquery = zorba.compileQuery("(1,2,3,4,5)")
  iter = xquery.iterator()
  iter.open()
  item = zorba_api.Item_createEmptyItem()
  while iter.next(item):
    print item.getStringValue()
  iter.close()
  iter.destroy()
  return

def example3(zorba):
  try:
    xquery = zorba.compileQuery("1 div 0")
    print xquery.execute()
  except RuntimeError, e:
    print e
  return

There may be C implementation in that list which can easily be bound to Python. Hope this helps, I was somewhat surprised to see so few implementations. Although, XQuery isn't the most desired of the XML tools I suppose.

Yaroslav Nikitenko
  • 1,695
  • 2
  • 23
  • 31
Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
3

You could use Jython to run the Python code -- that gives you access to some of the XQuery processors from the Java world. For example Saxon.

kohsah
  • 41
  • 1
0

Zorba 1.2 works from python. After installation you will get a python folder under zorba folder. Append it to sys.path, with zorba\bin folder also. After all manipulations import "zorba_api" will work!

vadim
  • 1
0

I had problems like Ted and tried to use answer from vadim. However, I had still problems to load zorba_api properly, complaining "ImportError DLL load failed" (not telling which one, using %1 as great nickname).

Finally, I got the solution:

Environment

  • WindowsXP
  • Python 2.6 installed at c:\Python26

Installation

  • Zorba 1.2 or 1.4 installed to standard location
  • Path to Zorba bin as first item in PATH
  • both files from Zorba bin\python (zorba_api.py and _zorba_api.pyd) moved to C:\Python26\LIB\site-packages

As result, I was able to run C:\Program Files\Zorba XQuery Processor 1.4.0\share\doc\zorba-1.4.0\python\examples\python_test.py from any folder in my computer, even without the python line, modifying PATH

NB:

  • PATH problem might be related to too long string there.
  • Process Monitor was of good help finding, which DLL cannot be loaded
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98