Do more with less : XQuery

It is my ambition to say in ten sentences what others say in a whole book – Fredrich Nietzsche

Few months back I had an assignment where I need to handle different XML files in Symbian. Back then, I used Symbian’s CParser. But as the XML files vary it is really painful and boring to write new parsing/validating code. One of my colleague was kind enough to write a parser, which I modified a bit to suit the requirement. But still it used to fail in some cases, so every new XML we parse, the rigorous we test. Since XML is one of the finest data representation techniques, I though cant we have a query language for XML. After googling I found something called XQuery.

XQuery is like what SQL is for databases. Its not SQL like syntax, but you can query any XML with a simple xquery. XQuery reduces so much of validation code. Following simple query creates a personal rss reader

declare variable $root := doc($source);
for $item in $root//channel/item
<li>{data($item/title) }</li>,
<a href=”{data($item/link)}”>More</a>

So how to execute it? Enter Qt. Qt has QtXmlPatterns module which provides xquery validator. Just run in your command prompt as

xmlpatterns -param source= rssquery.xq >> rss.html

replace the url with whatever feed you are intrested in. You can use any xquery validator, but I prefered to use xmlpatterns.

In Qt code you can use QXmlQuery to execute xquery. XQuery is a very handy xml validator, especially when used with Qt’s xmlpatterns module. You can also create xml files with it. I am not really sure about the performance (looks quite Qt in my tests), but it is really cool. You need not put all the boring validation flow inside the code, but have it seperate as a text file (with .xq extension) and run from you code. In my spare time I am writing a Qt based RSS Parser with XQuery as its back end just to flex my xquery skills. Hope I can share it soon!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s