so

XPath server

Volume 9, Issue 3; 16 Feb 2025

A little hack to leverage Saxon for evaluating XPath expressions in Emacs.

A few weeks ago, my attention was drawn to emacs-xpath, an emacs utility to evaluate XPath against XML. Someone (on Mastodon, I think) mentioned that it was unfortunate that it was limited to XPath 1 and that it would be cool to use Saxon for this and get all the goodness of modern XPath.

I do most of my XML editing in Emacs and I have occasionally wanted just this kind of feature. Not enough to seriously consider implementing XPath in Emacs lisp, but enough to consider it non-seriously!

If push comes to shove, I’m perfectly happy to fire up Oxygen to get the job done. Oxygen is a fantastic IDE for XML and it would be perfect but for the fact that it isn’t Emacs.

This client/server thing is a clever trick and it hadn’t occurred to me. I haven’t done much with networking inside Emacs and I had no idea it was as easy as it turned out to be.

As it happens, I built a web server environment into XML Calabash 1.x (I haven’t yet decided if it’s worth porting to my 3.x efforts) so I had most of the pieces I neeeded lying around.

All I had to do was wire them up to get xpath-server! (A quite pleasant Sunday hack and a nice respite from recent debugging efforts.)

Basically, you start the server in the background somewhere, load the lisp file into Emacs, and you’re off to the races. From an XML document, you can evaluate an XPath expression and get back a set of results. From the results buffer, you can quickly navigate to that location in the original file. Easy peasy.

There’s lots of room for improvement. Can I upload a set of files and search across them? Can I query JSON? Can I use XQuery or XSLT? Can I use Saxon EE? Can I use XProc? Etc.

Yes, of course you can, in principle. It’s just a simple matter of programming. But those aren’t likely to be things on the top of my todo list in the near future. OTOH, Sunday’s roll around every week.

In any event, I think it’s handy as it is.

#Emacs #Saxonica #XML