XPath & XQuery Tutorial for SQL Pros


Prior to starting at Altova I had zero experience with both XPath and XQuery. The first task I was presented with was to train myself on both query languages as quickly as possible and produce a concise video tutorial. It was important to develop a thorough understanding of their features and capabilities because both languages are integral to app development in MobileTogether and querying data in XMLSpy. I started with a strong background in SQL, learning XPath and XQuery by building queries first in SQL, and then determining how to replicate them in both query languages.

 

 

XPath & XQuery Basic Training

Altova has a comprehensive, free training library which covers XPath and XQuery. I found that I could get a strong understanding of both of these query languages simply by reading through this training documentation and applying the basic concepts to sample XML files included with XMLSpy. I found it quite amazing how intuitive it was to instantly query massive XML files using just XPath. As I started producing more advanced use cases, though, I began to understand that the queries built using XQuery were almost always much more concise and easier to read/debug.

As I read further into Altova’s training library I noted that the syntax of XQuery’s FLWOR expression was remarkably like that of a SQL query. XPath and XQuery, at their core, massage a dataset into smaller tables of data. They are both capable of filtering using complex conditionals and transformative functions. Additionally, both languages can query data from multiple sources joined together to produce result sets based on data in multiple normalized sources. Given the similarities that exist between these languages, it was clear to me that the simplest way for me to pick up XPath and XQuery was to work backwards from SQL. Armed with this knowledge, I set to work.

 

xquery and sql comparison

 

Tying it Together

MobileTogether includes a sample app called MyCar. This app demonstrates how to query a user about his or her dream car. The data is provided by a set of parameters defined within a database; it was ideal to work with as it contained many records which each had several data points. The dataset itself is provided as an MDB file which I opened with DatabaseSpy and exported to XML. I modified the exported file, changing a few of the fields to improve the readability of the queries I would produce.

Since both datasets were identical, I was able to produce SQL, XPath, and XQuery expressions that produced identical results. This made it very simple for me to quickly bring myself up to speed on both XPath and XQuery.  I found that I was producing complex queries in a matter of hours. I eventually set about producing the video below.  This video covers eight common querying scenarios, showing equivalent queries in all three languages.

 

export sql data

 

XPath & XQuery Training Video

Below is a link containing all the sample queries and datasets which were used in the video.  Grab a free trial of XMLSpy if you would like to experiment with them in the built-in XPath / XQuery editing window.

Sample Data & Queries

 

Tags: , , , , ,
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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