Introducing Developers to XQuery in MarkLogic

In the last few month’s I have helped a number of developers enter the world of XQuery.  Here are the first three websites that I point people to on their first day as well as some discussions on sequences and FWLOR statements that often occur.

Intro Sites

W3School.com’s XPath Tutorial and W3Schools.com’s XQuery Tutorial — While not directly related to the W3C, the XPath and XQuery tutorials here are a pretty good non-intimidating introduction.  (There is even a link to an XML tutorial for those who need some work on fundamentals)  Teaching XPath before XQuery is essential to stop people from overusing the “where” clause of FLWOR statements that could otherwise be done with an XPath predicate.  You can try out W3School’s code snippets first by inserting their  XML into MarkLogic under “books.xml” by copying something like the following into the Query Console

Once the person is comfortable with the basics, I almost always plunge them into the excellent Ninja Tutorial on the MarkLogic community site.  This tutorial reinforces XPath and XQuery while also expanding to things like JSON and search, which are nice to have on your radar early.

The next thing I do is make sure the developer has the most recent MarkLogic XQuery functions reference bookmarked.

The “Everything is a Sequence” Discussion

I can usually skip this discussion with people who enjoy reading language specifications with formal grammars or railroad diagrams (you can generate railroad diagrams for XQuery 1.0 with this excellent tool, use the tabs to page through the interface), but understanding sequences and where they fit into XQuery can really help new developers when they are reading and writing code their first day.

All literals are sequences, and don’t need parenthesis if there is only one item or if the expression is the Query Body of the Main Module

Sequences collapse.  Sequences don’t nest like Arrays in other languages. The following evaluate to equivalent sequences:

All expressions are sequences.  Literals, Path Expressions, FLWOR statements, IF/ELSEs , TYPESWITCHes, and COMPARISON all evaluate to sequences, even if they are just returning a sequence of count 1.  These language structures all return sequences, combine into sequences, and even have sequences inside of them:

Because FLWOR statements both contain and are sequences, you can quickly compose them:

Composing expressions of course quickly turns into the next discussion:

The “Think Like Functions” Discussion

When I see developers doing the following I usually start the discussion about functions and functional languages:

The problem is that XQuery is a functional language and variables like $accumulator are immutable.  That means that new memory is allocated for each assignment of an expression to the variable unlike Assembly or C which alter the actual memory address on assignment.  XQuery is just making multiple redundant copies.  The above code will work, but following this practice tends to lead to unnecessary performance problems if not simply inelegant code.  I try to get people to think of the XQuery syntax more like the math they learned in high school where functions can compose f( g( x ) ) :

Though eventually for style and maintainability purposes I’ll try to lead the developer to write the following:

The “XPath first” Discussion

XPath expressions evaluate to sequences, like just about everything else in XQuery.  XPath also allows quick filtering with XPath predicates, the thing in the square brackets.  As a result many simple FLWOR statements can just be reduced to a single XPath expression which I think encourages developers to move away from XQuery code that is limited to previous experiences with SQL.

I take issue with two things in the above code.  First, the FLWOR statement just returns the the iterator without doing anything with it or to it.  Second, the where clause could just as easily be done in a predicate.  In early versions of many XQuery interpreters (not just MarkLogic) predicates were significantly faster than where clauses.  This isn’t really true any more but I still suggest the following one line aleternative:


Updates:
Fixed URL link to Ninja tutorial (2012-05-04)

Comments are closed.