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 , the XML query language de jure. Eventually I want to run queries on MusicXML documents, but I need to start small.
While the W3C tutorial on XQuery is a great introduction, there's one little problem.
It doesn't really tell you how to implement : 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 in 10 Minutes: An Tutorial" that the Saxon XSLT processor I was already using for XSLT transformations already had an engine built in.
That's to say that the .NET version of Saxon has 2 command line executables:
- Transform.exe, which I'd already used for XSLT transformations
- Query.exe, which allows one to run queries
So much for paying attention to what I download …
From there, it was a simple matter to use for the first time.
Here are the steps:
- I downloaded the books.xml file provided by the W3C and place it into the "bin" directory of Saxon on my drive. This is same directory where the 2 afformentioned executables reside.
- Using the kick-tail text editor jEdit, I copy/pasted/saved this query example from the W3C as "test." (also saved in the "bin" directory):
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>
This query simply lists all the titles from "books.xml" in alphabetical order.
- 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." 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.
- Now I was in the correct folder and could run the with the following command line syntax:
query test.
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> Kick Start</li></ul>
into a new document in jEdit anyway.
We're gonna take care of those line breaks now …
- 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> Kick Start</li></ul>
So I just invoked the jEdit search/replace box and did the following:
This simply says:
Find all instances of
><
and
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"?>
<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li> Kick Start</li>
</ul>
Problem solved.
- Now I simply saved this document as "test.html" and opened it in a browser.
Anyway, that's my very simple start to , but I'm feeling pretty good about it nonetheless.
Nice!
For line break, the name is “indent”. You can have them right from query.exe I think.
Try
query test.xquery !indent=yes
Out of curiosity, what’s your plan with MusicXML and Xquery?
lasconic
13 Sep 09 at 3:58 am
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
https://audiveris.dev.java.net/
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.
nitin
13 Sep 09 at 4:15 pm