Friday, August 17, 2007

Using XQuery and the slash operator to quickly try out XPaths

This is the coolest thing I've seen in a while...

In XQuery you can constuct a node just by writing it, eg:

<node>I'm a node</node>

and then you can use slash operator to apply an XPath to that node:

<node>I'm a node</node>/data(.)

returns "I'm a node"

The XML doesn't have to be limited to a single node - you can do:

<root>
  <node>foo</node>
  <node>bar</node>
</root>/node/data(.)

...to get "foo bar".

Or:

<root>
  <node>foo</node>
  <node>bar</node>
</root>/node[1]

to get:

<node>foo</node>


Using this technique in combination with Kernow's XQuery Sandbox makes it straightforward to paste in some XML and start trying out some XPaths.

4 comments:

chris wallace said...

Andrew, you can do the same with the
eXist XQuery SandBox

This also does syntax-checking as you type, making it a good interface for teaching.

Andrew Welch said...

That shouldn't be too hard to implement in Kernow... I'll give it a go.

(If you weren't aware, I'm an ex-student of yours! I graduated in 2001 - iirc correctly you taught us things like Rational Rose and function points but it was 5.30 on a Friday so my memory is hazy... It's all Agile now anyway :)

chris wallace said...

I'll take a look at Kernow for teaching too.

(Well , good grief,that's amazing! I'd seen your blog before when I was working on an XQuery Suduko solver but made no connection. My teaching seems to be even more forgetable than you suggest since I certainly didn't teach function points. in 2001 I was somewhere in the Indian Ocean so I guess you must have suffered from me in the first year in 1998-99. That must have been Systems Development with web development in Perl, or maybe Systems Design with Java?)

Andrew Welch said...

Kernow is now available using Java Web Start - and I've added code highlighting and syntax checking as you type to the XQuery Sandbox tab. Hopefully this is as-good-as if not a little better than the sandbox provided by eXist website:

http://kernowforsaxon.sourceforge.net/jws.html

Let me know about any feature requests or bugs - now's a good time as I'm relatively free for one more month.