What is XQuery?


Mockup of an XQuery file.

XQuery is the primary language for querying XML documents and similar data formats. As a superset of XPath, XQuery uses similar path-based expressions to navigate the document tree structure. In fact, a XQuery script is simply an expression that evaluates to a value. More than just a query syntax though, XQuery also offers functional programming and the ability to construct new XML documents.


XQuery is not the only query language for XML. There is already XSLT which can both style and transform XML documents. However XSLT relies on pattern matching, as oppsed to driectly querying data. As one user put it: "you're not really querying for certain data, but actually “hoping” data will fall within your pattern". Since XQuery shares similarities with SQL it is much easier and more natural to learn and use.


Should I Use XQuery?


XQuery is a W3C recommendation, meaning it is highly compatible with other W3C standards. XQuery supports XML manipulation natively, which makes it a great alternative to JS. XQuery code is also much simpler than JS, resulting in fewer lines of code and easier readability.


On the other hand XQuery is not as robust as some of the alternatives which have larger, more-active development communites and a breadth of implementations and extensions. Unlike how SQL can modify a database, XQuery does not natively support updating XML documents. While this functionality can be extended by XQuery Update Facility, it may not provide the same flexibilty as a traditional relational database with SQL. Full-text search can be enabled with the XQuery and XPath Full Text extension.


Using XQuery


So how do we use XQuery? Suppose our bookstore has a catalog of books with several categories, and each book having a title, author(s), and price like so:



We can use XQuery to derive lists of books based on their attributes. For instance, we can find the title of all books where the price is less than $30. In XQuery we start by setting a variable that holds each item returned from the path “/bookstore/book” inside our XML document.



Next, we attach a predicate or (using FLWOR expression) a where clause that selects only the returned items that have a price less than 30.



Finally, we return the title of each returned item that passed the where clause, in alphabetical order. We can easily add in more clauses (such as one that targets a certain category of books) or choose a value by which to order the result.



XQIB (XQuery in the Browser)


XQIB enables client-side programming in XQuery, similar to the way we use Javascript. It does require XQIB JS (part of MXQuery) to be installed on the server. The installation process is simply a direct upload; no configuration is required. Once the MXQuery engine is launched you will be able to use the browser api, allowing for DOM and CSS access, Event Listening, REST, and some helper functions.


References:


What Is XQuery

https://www.xml.com/pub/a/2002/10/16/xquery.html

SourceForge MXQuery Repository

https://sourceforge.net/projects/mxquery/

XQIB (XQuery in the Browser)

http://www.xqib.org/

XQuery vs XSLT

https://www.javatpoint.com/xquery-vs-xslt