Widgets, Gadgets, Toys, Design and Technology by Adam Bidwell...

XPath Test Tool

Ok, so this one is not a "toy", but more of a tool really. It is quite common these days to be building web, desktop or mobile applications that store some kind of data in an XML format, and those in the know will be aware that this eventually means using something called XPath. This is a tool to help perfect your XPath queries, so that your application is selecting no more and no less than the required data from your XML structure. I'll give a little background to the application below, for those that want to skip all that, click here to go straight to the download.

XML? XPath?

To rewind a bit, XML is a tree-like structure. It has a root 'node', and then 'child' nodes that spring off of it. A piece of example XML may look like this:

<catalogue>
    <book>
        <id>1</id>
        <title>Tuesdays with Mantu</title>
        <author>Rich Siegel</author>
    </book>
    <book>
        <id>2</id>
        <title>Mr. Sponge's Sporting Tour</title>
        <author>R.S. Surtees</author>
    </book>
    <book>
        <id>3</id>
        <title>Molesworth</title>
        <author>Geoffrey Willans and Ronald Searle</author>
    </book>
</catalogue>

When writing an application, it becomes necessary to be able to locate specific parts of this XML. For example, you may wish to find out the 'title' of book number 3. This is where XPath comes in.

An XPath query, in its simplest form, is a list of the node names in the tree en route to the required data. So, using our example above of selecting a book title, the beginnings of the XPath query would be /catalogue/book/title. However, this would return all three titles seen in the XML above. What we want is a specific title.

In order to narrow this down to a particular item, in this case the book with an id value of 3, we must add conditions to the query. We can do this by specifying a condition in the query, inside square brackets []. So, if we quickly revise our query to select the required book, it would be /catalogue/book[id=3]/title.

Makes sense? Not to worry if it doesn't yet, this article is more to provide you with a tool to use while you are learning. There are several very good resources on the web which will help you go further with this, and you can find two of these listed in the footer of this article below.

Get on with it!

Ok, so enough background, here is the tool. To install it, download it from the link below. It is quite simple in operation, you basically select an XML file to query, enter an XPath Expression and then click 'Single Node' or 'Multiple Nodes'. The 'Single Node' button will return the first node that matches your expression, the 'Multiple Nodes' option will list all the nodes that match your expression.

I hope the application is useful to you, please get in touch if you have any suggestions, feedback, etc.

CLICK HERE TO DOWNLOAD

 

Further reading:

The excellent W3Schools has a good tutorial covering the basics of XPath, with a simple online evaluator
http://www.w3schools.com/XPath/xpath_intro.asp

O'Reilly have also published chapter 9 of their book "XML in a nutshell", which contains a very good explanation of XPath:
http://oreilly.com/catalog/xmlnut/chapter/ch09.html

screenshot: