Monday, August 17, 2009

xquery script

The other day, I really wanted to be able to run an xquery expression over some xml and use it as some scripting input... I was really surprised to see there really doesn't appear to be a nice way to do this!

After all... xquery is the xml equivalent of grep. Imagine! Landing in an OS with no grep equivalent!

Oh, wait... I just described windoze...

I wrote a small piece of XLS wrapped in a script( xsnap.sh) the script got a slightly more complicated once I found out that if an input document has a namespace defined, you have to use it in your expression.

Now this is handled like magic, but can be turned off by uncommenting the line "XSNAP_SMARTY" then you can specify "xsnap:expression" to match the default ns of the document.

Here is the dealio: xsnap.sh "groupId/text()" pom.xml
junit
hsqldb
...

And viola! A "grep-like" tool for xml!

This mechanism is more robust that just grepping xml since it uses the xquery standard junk.