Part of my new gig includes working with a Mark Logic content server, which is basically an indexable, query-able, heavily indexed collection of XML documents.
When I first heard this I shuddered at the thought of having to take on XSLT again, which is one of those technologies I want to love but have never felt comfortable navigating in. To my great relief, Mark Logic doesn’t use a bit of XSLT, and instead relies on the relatively new (to me) xQuery.
XQuery is a LISP like, expression only language. It has some SQL like constructs, specifically a for/let/with/order/return expression (FLWOR) that if similar to SQLs SELECT/FROM/WHERE/ORDER statements.
Like XSLT, xQuery relies heavily on XPath to pare down a source tree to the sections you want to operate on. Unlike XSLT, the xQuery programming language looks like a traditional C/Perl style language (sort of). In other words, the syntax of the language itself isn’t in XML, which I think was one of those things that always served as a personal brain block. “Is this bit of XML I’m writing XSLT, or referencing my original document, or referencing the new document I want to create.
Here’s a quick example of an XQuery FLWOR
for $books in doc("books.xml")/library/books/computer
let $title := $books/title
where $books/subject = "PHP"
return <php-books>{$title}</php-books>
...
I haven’t had this much nerdy fun with a new technology is a long time.
Useful Tools
The O’Reilly book has proven to be an invaluable resource here. It covers both practical examples and a deeper understanding of the language itself (typing system, implicit conversions, etc.)
As far as editors/tools go, Stylus Studio appears to own the commercial market, but there are some free tools available which are serviceable. I imagine Eclipse has a way of handling XQuery as well, but then you don’t have any RAM left over to do anything else.
For editors I’ve returned to jEdit, specifically for the syntax color coding (the .xq extension) There is a jEdit plugin that allows you to apply an xQuery program to a source document, but so far I prefer the Native OS X application XSLPalette. There’s also a TextMate plugin by the same author, but I’m an old crank who still sticks with BBEdit. If you’re using Mark Logic itself like I am, there’s a plugin for jEdit that will let you query your content store directly. I haven’t had a chance to play with this myself, but it looks promising.
Finally, both of the above programs will let you specify a single input document to use in the query. Almost all the examples I’ve seen online assume you’ll be loading your input document using the doc() function, so here’s the above example written with the assumption you’ve set the input document to books.xml
for $books in /library/books/computer
let $title := $books/title
where $books/subject = "PHP"
return <php-books>{$title}</php-books>
...