XMLSpy XPath Analyzer

The following XMLSpy tip was taken from the blog of Altova CEO, Alexander Falk.

One of my favorite features in the Altova XMLSpy XML Editor continues to be the XPath analyzer, which lets you quickly locate information in any XML file by typing an XPath 1.0 or 2.0 expression. Introduced first by Altova in 2001, the XPath analyzer gained XPath 2.0 awareness in version 2004 and was turned into a new dynamic window with multiple output tabs in version 2007.

The XPath analyzer is really convenient and easy to use. Located within the bottom information/output window, the XPath analyzer is found in a separate tab, right next to the Validation results and the Find in Files function. You can configure it for XPath 1.0 or 2.0 with the click of a button, and can immediately see the result of any XPath expression, by typing it into the text input line - the XPath expression is evaluated immediately and the resulting nodeset is displayed in the output window:

In this example I have used the XPath expression //expense-item[@type="Lodging"] to locate all <expense-item> elements that have a type attribute with the value of "Lodging".

The resulting nodeset is evaluated dynamically as you type, so you can always see whether or not you have entered a correct XPath expression (in addition, the text entered turns red, if there is any error in the expression). You can quickly navigate to any of the found nodes by clicking on them, and the corresponding node in the document will be highlighted in the main editing window (irrespective of which view you are using).

One of the cool new features that was just added in the new XMLSpy 2008 version this month is the ability to also evaluate XPath expressions across multiple files. This is great if you are working on a larger project and need to quickly see where certain information is located. Sure, there is always Find in Files, which has powerful Regular Expressions, but a true XML Aficionado always prefers to use XPath :)

In this screenshot I was searching for all elements in any of my project files that have an xml:lang attribute - this can be expressed in XPath as //element()[@xml:lang] - and the resulting nodeset shows nodes found in several different files in my project. I can, of course, open any of these files and immediately view the found nodes just by clicking on them.

Here is another nifty trick: if you often work with larger files and want to persistently bookmark certain positions in those files, you can also use the XPath analyzer to quickly navigate to those locations. All you need to do is add comments to the XML file to mark those locations, e.g. <-- Bookmark #1 --> and then you can use the XPath expression //comment() to show all comment nodes in your file. Now you can click on each one and you will see them displayed in the editing view. You can even quickly reestablish bookmarks by clicking on each one and hitting Ctrl-F2 to set a new bookmark: