Click here to Skip to main content
Licence 
First Posted 1 Dec 2003
Views 75,402
Bookmarked 27 times

Debugging XPath Queries

By | 1 Dec 2003 | Article
An application to visualize XPath Queries

Sample Image - screenshot.jpg

Introduction

This is a small application I wrote when I was stuck in front of an XPath query that I just couldn't understand. Took about an hour and a half to whip it up. The idea is to let you execute a query and see for yourself the results that are returned.

There are other tools that does it, but either they are Java based and require > 15 Mb support libraries or they are web based (which I hate).

The Code

The application gets its input from one of two sources - a file or the clipboard, loads it into an XmlDocument and start executing queries against it, showing you the result on the way.

I didn't built it to be performant or correct, just to do the job, so use it the next time you've XPath problems, and you'll like it. The other solution that I found was XmlSpy at 300$ +

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

goerge@Mailinator.com



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGenerating xpath add-on (quick and dirty) [modified] PinmemberMikeEast22:24 16 Jan '08  
Hello all!
 
Lazy as i am, I was looking for a solution to get an xpath expression from clicking on an element in an XML DOM.
 
This article was a great start and here is my simple add-on:
 
private string calculateXpath()
{
    if (XmlTree.SelectedNode != null)
    {
        List<string> xpathItems = new List<string>();
        XmlNode node = ((TreeAndXmlNode)XmlTree.SelectedNode.Tag).xnode;
        while (node.ParentNode != null)
        {
            int index = 0;
            XmlNode currentNode = node;
            while (currentNode.PreviousSibling != null)
            {
                if (currentNode.Name == node.Name)
                    index++;
                currentNode = currentNode.PreviousSibling;
            }
            xpathItems.Add(string.Format("{0}[{1}]", node.Name, index));
 
            node = node.ParentNode;
        }
        xpathItems.Reverse();
        return string.Format("/{0}", string.Join("/", xpathItems.ToArray()));
    }
}
 
It generates xpaths like: /catalog[1]/dvd[14]/title[1]
 
It would be nice to get xpath based on other stuff than index, but this is what I needed. Developing this further is up to you.
 
Cheers!
 
::m
 
modified on Thursday, January 17, 2008 4:37:39 AM

Generalin VB.NET Pinmembery2kvirus12:50 22 Aug '05  
GeneralWrong topic Pinmemberalerma21:48 14 Dec '03  
GeneralGood job PinmemberBill Seddon21:21 9 Dec '03  
GeneralRe: Good job PinmemberMarc Brooks8:45 29 Jan '04  
GeneralRe: Good job PinmemberBill Seddon8:55 29 Jan '04  
AnswerRe: Good job PinmemberMarc Brooks5:59 10 Oct '06  
GeneralGood work, but... PinmemberChristoph Lederer21:19 9 Dec '03  
GeneralRe: Good work, but... PinmemberEdneeis18:59 3 Feb '04  
GeneralGraphical XPath Queries Pinmemberchetmurphy18:37 9 Dec '03  
GeneralRe: Graphical XPath Queries PinmemberMarc Brooks8:48 29 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 2 Dec 2003
Article Copyright 2003 by goerge@Mailinator.com
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid