on using XQuery for the first time

Obviously, I've been playing around with XSLT lately. So naturally, the next logical step was to delve into XQuery, the XML query language de jure. Eventually I want to run queries on MusicXML documents, but I need to start small.

While the W3Schools tutorial on XQuery is a great introduction, there's one little problem.

It doesn't really tell you how to implement XQuery: i.e. how to actually run a query and retrieve results.

So after some random perusing and downloading, I – like the fool I am – was made aware by Dr. Michael Kay's "Learn XQuery in 10 Minutes: An XQuery Tutorial"  that the Saxon XSLT processor I was already using for XSLT transformations already had an XQuery engine built in.

That's to say that the .NET version of Saxon has 2 command line executables:

  1. Transform.exe, which I'd already used for XSLT transformations
  2. Query.exe, which allows one to run XQuery queries

So much for paying attention to what I download …

From there, it was a simple matter to use XQuery for the first time.

Here are the steps:

  1. I downloaded the books.xml file provided by W3Schools and place it into the "bin" directory of Saxon on my drive. This is same directory where the 2 afformentioned executables reside.
  2. Using the kick-tail text editor jEdit, I copy/pasted/saved this query example from the W3Schools as "test.xquery" (also saved in the "bin" directory):

for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>

This query simply lists all the titles from "books.xml" in alphabetical order.

  1. Then using jEdit's command line plug-in called "Console", I set Console to the Saxon "bin" directory where "query.exe", "books.xml", and "test.xquery" reside. The easiest way to set the directory in Console is to type:

cd "C:\Documents and Settings\nitin\Desktop\saxon\bin"

Of course, you might extract Saxon elsewhere, but the important thing is to type cd + opening quotation mark + full path to Saxon's "bin" folder + ending quotation mark.

  1. Now I was in the correct folder and could run the XQuery with the following command line syntax:

query test.xquery

And my results look like this:

I know what you're thinking: no line breaks! Sure, the computer doesn't care, but this is really hard for humans to read!

Yes, that's true. But I went ahead and pasted the following:

<?xml version="1.0" encoding="UTF-8"?><ul><li>Everyday Italian</li><li>Harry Potter</li><li>Learning XML</li><li>XQuery Kick Start</li></ul>

into a new document in jEdit anyway.

We're gonna take care of those line breaks now …

  1. One of the many great things about jEdit is the ability to run Beanshell commands, which despite my attempts to sound authoritative, I only learned about roughly 30 minutes ago. This means that a search and replace can be done in jEdit using simple Java syntax to fix that line break issue. The first step is identifying where to insert the line break. I needed it in between > and <. Specifically, I needed a line break between all the red and green colored brackets:

<?xml version="1.0" encoding="UTF-8"?><ul><li>Everyday Italian</li><li>Harry Potter</li><li>Learning XML</li><li>XQuery Kick Start</li></ul>

 So I just invoked the jEdit search/replace box and did the following:

This simply says:

Find all instances of



Replace it with



– i.e. the text between the quotation marks. The n is, by the way, the line break syntax.

When I hit "Replace All", this was the result:

<?xml version="1.0" encoding="UTF-8"?>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>

Problem solved.

  1. Now I simply saved this document as "test.html" and opened it in a browser.

Anyway, that's my very simple start to XQuery, but I'm feeling pretty good about it nonetheless.


Related Content:


  1. lasconic

    For line break, the name is "indent". You can have them right from query.exe I think.
    query test.xquery !indent=yes

    Out of curiosity, what's your plan with MusicXML and Xquery?

  2. nitin

    The indent option works! The output is well-nested XML now, too. Thanks for the great tip.

    I haven't formulated my thoughts on MusicXML/Xquery too much, but I'd really like to see digital libraries start OCRing their scanned sheet music and then work toward search capabilities for those collections. Furthermore, I'd like to see a non-textual search interface for querying pitches and patterns, etc. i.e. typing in search queries on a staff line. Maybe advanced searches would require text-based input, but it would be good from the "cool" factor POV to have staff entry for basic queries.

    So if universities and even open-web projects got underway we could start playing catch-up with the articles repositories. I haven't had much luck running Audiveris
    but we're gonna need a free, open-source music OCR program if this kind of thing could ever take root on the web.

    BTW: thanks for being my first commentor! 🙂

    Update 091509: jEdit has an XML plug-in with an "Indent XML" command. But it's still likely best to use the indent option while running the query as suggested by the great comment above.


Leave a Comment

Your email address will not be published. Required fields are marked *