running XQuery online [Mon, 09 Nov 2009 01:26:54 +0000]
A while ago, I posted about my first experience with XQuery [http://blog.humaneguitarist.org/?p=152] and how I'd used the .NET version of the Saxon [http://saxon.sourceforge.net/] processor on my local Windows machine.
Obviously, I want to extend that experience to running XQueries online. So far, I know this can easily be done with a native XML database server like eXist [http://exist-db.org/]. That is to say, when I installed eXist on my local machine and made it go live: bam! - instant XML server with built-in XQuery functionality, accessible from anywhere.
Another way is to use one of the more popular web-scripting languages to execute XQuery syntax. I nearly killed myself this weekend trying to install the Zorba PHP binding for XQuery [http://www.ibm.com/developerworks/opensource/library/x-zorba/index.html] (i.e. run XQuery natively from within a PHP script). I just couldn't get all the dependencies successfully installed on my virtual install of Ubuntu Netbook Remix [http://www.ubuntu.com/getubuntu/download-netbook?info=EXLINK] (BTW: I use Sun's VirtualBox [http://www.virtualbox.org/] for virtualization). Perhaps I'll be able to make it work another time.
Now, even though it makes all the sense in the world to stick with a native XML server like eXist if I want to make a large collection of searchable XML documents online (and I do), I'm feeling non-sensical. What I decided to try was to run my computer as a more traditional server using the X-Apache-MySql-PHP model, specifically WampServer [http://www.wampserver.com/en/].
From there, I placed my sample document, "books.xml [http://www.w3schools.com/XQuery/books.xml]" and my "test.xquery" file from last time:
for $x in doc("books.xml")/bookstore/book/title
order by $x
in WAMP's "www" directory - i.e. the directory which is accessible from the browser, the place where one would put all their HTML files, etc. for the world to see.
Of course, I was still missing the actual XQuery processor at this point. What I tried is to put the relevant executables for Saxon in the "www" directory as well.
... Now, I'm sure this is totally unsafe or something, but I was just testing and I only make my computer "go live" as a server for short periods of time.
Anyway, from there I used PHP to call the Saxon processor and to display the results in the browser. It actually worked!
Here's the code:
echo "Hi. I'm going to use XQuery to list the books alphabetically.";
exec("query.exe test.xquery !indent=yes", $results);
foreach ($results as $value)
You can see that the PHP "exec" command called the Saxon executable named "query.exe" and executes the "test.xquery" file.
It saves the results in a variable called "results" and then prints each value of the results in the browser.
For now, I'm OK with this, but I need to eventually do the same thing with the Java version of Saxon, I suppose, if I'm ever going to run this on a Linux server. It shouldn't present any new hurdles, but I need to try it to make sure, of course.
If anyone out there has any thoughts or recommendations on a more elegant method to achieve these results - and what the security risks of the approach I've outlined presents (since I didn't use a CGI-bin), please speak up. I'm all ears.
Update, November, 2009: No problems with using the Java version of Saxon. I did place it in a "bin" directory on my local server so that the Java file wouldn't reside in the directory that users have direct access to.