xQuery access to eXist database

2010-05-08T08:27:35
Dave Pawson.  link
Home

xquery

I've not used xQuery much in the past. Looked at it. Thought it pretty ugly compared to XSLT for what I do and left it at that. Separately, I've always had this niggle that XSLT has failed to address the needs of people processing either a large number of small files or a number of very large files. The consensus seems to be that for a 1Mb file I need 10Mb of memory. So given a reasonable system, 4Gb of memory, some part used for OS and java etc, we are restricted to processing 350Mb files. I would never play with such files, but dataheads seem to, either as a collection or even individual files. I scratched that itch yesterday.

I have been enthralled by XRX as an idea. Playing with that I installed the eXist db some time back and was impressed by the ease with which I could load data and search it, using their sandbox toys. This time I installed it with the intention of accessing the database from outside exist. I stumbled with the xQuery syntax, but eventually found a solution, below. I'd stored about 50 XML docbook files, each namespaced.

xquery version "1.0" encoding "utf-8";
declare namespace  xdb="http://exist-db.org/xquery/xmldb";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";

declare namespace db ="http://docbook.org/ns/docbook" ;
declare variable $src as xs:string := "/db/mycollection";

xmldb:login($src, "admin","mypasswd"),
<doc>
{
for $title in doc("0909.xml")/db:chapter/db:section/db:title
      return 
<db:s> {$title} </db:s>
 
}

 </doc>

Soem of this came from the eXist documentation, but I found it quite hard work. Mostly my troubles were with xquery, having no reference. The xmldb namespace prefix is access to one of the exist library of functions which are extensive, but appears to give no higher level 'this is how to use them' advice. The login function I take to be an external java call, though I can't be sure. That provides access for the http session. the $src variable accesses the database, the collection I created (same as an XSLT collection) which is used as the context for the query, I think. The rest simply pulls all the section titles from one document in the database. It would readily extend to iterating over the entire collection of documents.

Nice and simply, quite flexible and seems to do everything I wanted. Now I wonder if I can do that with XSLT 2.0?

Keywords: xquery

Comments (View)

Return to main index