I don’t know about you, but I get excited reading papers like Dimitre Novatchev’s Programming in XPath 3.0, and want to try some of XPath 3.0’s new features for myself. I also want to build upon some of the examples given there.
But to do that, you need an XPath 3.0 processor. And it turns out, they’re harder to find than XPath 2.0 engines. (Not too surprising, since 3.0 is more complex, and has just reached official Recommendation status in the last 3 months.)
Saxon HE, the free version of the definitive XSLT/XQuery processor, does not do XPath 3.0. Saxon PE and EE do. They are not free (starting at £50.00 at the moment), though you can apply for a 30-day trial license. My trial license arrived pretty quickly today after I requested it. However that does put you under a time limit, and if you’re like me, picking up projects and putting them aside all the time, the clock may run out before you’re done experimenting.
XmlPrime is listed on the QT3 Test Suite Results Summary for XPath 3.0, so it sounds like it implements XPath 3.0, right? But the version of XmlPrime listed there is 220.127.116.11, whereas the most recent version that seems to have been released publicly is 2.9. And that version doesn’t claim to run XPath 3.0 or XQuery 3.0. So where do we get our hands on the version that does?
However I found out (via Dimitre) that BaseX includes XQuery 3.0, which should by definition include XPath 3.0. (Why then is BaseX not listed on the implementations of XPath 3.0 in the QT3 Test Suite Results Summary? It’s listed for XQuery 3.0… Hmm.)
If BaseX does run XPath 3.0 (and yes it does seem to… but see caveats below), it also has the advantages of (1) being free, and (2) having a GUI front-end that makes it easy to experiment with different queries. A minor disadvantage is that it seems a bit daunting to be installing a full-blown XML database server, if all you want is to try some XPath. But disk space is cheap, and honestly, my low-end laptop didn’t have trouble running BaseX along with several other applications. Moreover, it wasn’t complicated to set up and start evaluating XPath.
So here’s the procedure, as I did it.
- Download and install BaseX. I’m on Windows, so I used the Windows Installer (v. 7.9).
- Following the Getting Started instructions, launch the GUI. I didn’t have the installer create desktop icons, so I had to type basexgui.bat in the Windows 8.1 Start menu. Windows found the .bat file and launched the GUI successfully.
Alternatively, you can use the command line or the client/server scenarios, but I think the GUI’s ideal for experimenting and development.
- You can’t (AFAIK…) run XPath (or XQuery) without having an input document. So, following the GUI instructions, create an XML database. This consists of specifying an XML document to connect to as the database.
There’s a suggestion to use BaseX/etc/factbook.xml as a sample, which is a substantial one (1.25MB) and good for many cases; but if your use of XPath tends to ignore the input document (like most of Dimitre’s examples), you may want a tiny document like the included w3-catalog.xml.
The document structure pane in the upper right shows a really cool representation of the XML tree, to which a tiny document does not do justice.
- Try out a simple XPath (or XQuery) expression in the “XQuery Panel.” To do this, click on the dropdown that says “Find” and change it to “XQuery.”Then enter an expression in the text field next to it. E.g. try one of these…
- 5 + 6
- count(//*) (: returns 4, for w3c-catalog.xml 🙂
- let $i := 3 return $i
You should be seeing the expected output in the Result pane (lower left quadrant).
You also get some nifty profiling information in the lower right pane.
- Now you want to try more sophisticated queries? Type/copy/paste your query into the Editor window (below the XQuery Panel on the left). You can also open your XPath/XQuery files using the file browser on the left side. Caution: if your query code filenames don’t match the default filters (*.xml and *.xq*), you may get frustrated trying to find them, even though the file browser says it’s showing “All Files”. First go to Options>Preferences, check General>Use Simple File Chooser, and add *.* to Editor>File Filter. After I did that, I was able to find my files.Press Ctrl+Enter, or hit the green play button, to run your example. Again, the results appear in the Result pane below the Editor.
So now we’ve achieved our goal: a working XPath 3.0 processor. Right?
Basically, yes. I had trouble running some of Dimitre’s examples, e.g. when declaring recursive functions. I seemed to get an error whenever a function parameter was declared as function() without declaring the return type of the passed function. I’ll probably submit a StackOverflow question about that. But other examples worked well, e.g. using the simple mapping operator:
('London', 'New York', 'Paris') ! string-length()
or creating elements in XPath:
name(parse-xml(concat('<Person>', (//Author)[last()], '</Person>'))/*)
The examples on closures and partial application also worked fine.