Tuesday, September 25, 2007

XMLSpy Tip: Using the XPath analyzer to quickly find information in files

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 Validaton 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:


To see more useful information about the XPath analyzer, watch this cool flash video or read the XPath Evaluation chapter in the online manual.

4 comments:

Jerry Sheehan said...

Excellent tip for linking into large documents using XMLSpy! This really helps! Now I don't have to add bookmarks anymore everytime I open my large files. I'll just add comments and use the xpath you provided to toggle through my large files.

Hey.. Not only that, I can use this feature in the Grid View as well. This is even better.

Thanks for sharing the tip!!!

spiff said...

The XPath analyzer tab is indeed a cool feature in XMLSpy.

Speaking about bookmarks I have an additional tip: The File Commander PRO plugin for XMLSpy can save and restore bookmarks for you whenever you open a file in Text view.

Just take a look here to learn more: Workspace support

Regards

A.Falk said...

Hi spiff:
your File Commander PRO and other plugins for XMLSpy sound cool - can you get me an evaluation license so I can take a closer look at them?
Thanks,
A.Falk

spiff said...

Hi Alexander!

Great that you want to take a look a the plugins for XMLSpy!

There is no need for an evaluation license. Just run the installer and the next time you start XMLSpy the plugin is available for a 14 days trial period.

If you have any questions just drop me a line at support@spycomponents.com

Regards