Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

Intro to XPathmania - Extending Visual Studio 2005 to Support XPath Development

Something that has been on top of my list of improvements needed for XML in Visual Studio is better support for writing and testing XPath queries.  Currently, the only way to write and test your XPath statements is to either create your own console or Window application, create a test XSLT (using the XSLT debugger), or worse, put break points in your code, and use the edit and continue feature.  Well, I heard that Visual Studio extensibility has been greatly improved in Visual Studio 2005, so I figured that I would attempt to extend the XML Editor (yes Virginia there is an XML Editor in Visual Studio) to include support for XPath.  The project is called XPathmania and is part of the open source Mvp.Xml project (www.mvpxml.org).  The XML MVPs are in the middle of moving our code base to CodePlex, but since this is a new project under the Mvp.Xml umbrella, all of the code and releases are already hosted on CodePlex MvpXml page.

In this, the first version, I decided to keep things pretty simple by just creating a Visual Studio Tool Window where a developer can write an XPath statement and test it against the current document in the XML Editor.  After running an XPath query it will list and highlight the nodes returned from the query.  In a future version, I would like to see Intellisense, adding schema awareness, and even some code snippet generation.  You can download XPathmania 1.0 Release Candidate 1.1 as an install package, and/or if you want to see how it is done, you can download the source code  (you will need to install the Visual Studio SDK – April 2006 release if you want to compile it yourself).  After you install XPathmania or compile it yourself you will have to tell Visual Studio to display the XPathmania tool window thru the View|Other Windows menu list and select XPathmania.  XPathmania will then be a floating window at this point, but you can click on the title bar and then dock it.  I prefer docking it to the bottom of Visual Studio, but you can place it just about anywhere that makes sense to you.


Image 1- Displaying the XPathmania Tool Window


Image 2 – Docking XPathmania

Now that we have the tool window displayed, let’s see what it can do.  To run a query against an XML document, you are going to need a document.  In this case, I’ll create a new document that contains the information that I wish to query, a listing of compact disks with the artist name and the title of the CD.  I wish to find all CDs in this document for the artist Buckcherry, and the XPath statement is //music/cd[artist/name='Buckcherry'].  If we enter the query into the XPath Text Box and hit enter (or click the Query button), the query will execute and the nodes that match the query will be highlighted in the XML Editor, plus they will be listed in the XPathmania Tool Window.


Image 3 – Executing a query

If you double click on a results row, it will automatically select that node in the XML Editor (and even scroll to that node if it isn’t currently in the viewable area).  Plus, if you edit the XML document, the results in the tool window are kept in sync with the XML document.  So if you move a node, or delete the node, the new information will be reflected in the Results grid.  Note – any new nodes added to the XML document will not be reflected in the results.  To see the results based on the latest version of the XML document, re-execute the query by clicking on the Query button.

But, we all know that XML without at least one namespace is not much better then delimited text files (aka naked XML), so we should add a namespace to this XML document.  This way there is an identifier in the XML document that can help the consumer what type (schema or structure) the XML conforms to.  We can do this a number of similar ways, but let’s pick the easiest, by adding the attribute xmlns=”urn:mvpxml.org/music” to the music element, and run the XPath query again.  But this time, instead of getting the same results as before, we get an Error Tab with the message “Document has a default namespace.  Did you make sure to add it to the Namespace Table and use its prefix in your XPath query?”. 


Image 4 – Default Namespace Error

What gives?  All we did is add a namespace to the document.  Well, the reason is due to this little back corner of the XPath spec.  You can read the details for yourself, but in the end, you need to add a way to map prefixes in your XPath query to namespaces (both in your query and in the XML document).  This is one of the most common questions on the newsgroups, and hopefully folks will use this tool to learn to write XPath queries, correctly.  We can do this by adding a little guidance to the error messages (and eventually intellisense).

So, let’s do like the error message suggests and add the namespace to the Namespace Table.  Just click on the Namespace Table tab and enter the prefix “a” into the first column and the namespace “urn:mvpxml.org/music” to the second column (you can’t leave the prefix column empty, otherwise you will get another error message).  Then add the prefix to each element in your XPath query, so it looks like this: //a:music/a:cd[a:artist/a:name='Buckcherry'] and then execute the query again.  Great, now we got the results that we were looking for.  But what if the XML document doesn’t use a default namespace and the namespace used the prefix “b” instead of nothing (the default)?  Let’s go in and change the document to match this scenario and run the exact same XPath query using the prefix of “a”.


Image 5 – XML with Namespace and Prefix

 

Wow, the query still works!  The XPath Data Model only uses the prefix as a sort of quick way to refer to the full namespace.  This way the prefixes used within the XML document does not have to be the same ones used in the XPath query, which is good, since the system that generated the XML doesn’t have always use the same prefix (although you will normally find that they do use the same prefix, but they do not really have to).

For this version of XPathmania, that’s about all it can do, execute some XPath queries and give a little guidance on handling default namespaces.  There are lots of other possible enhancements that I can think may be of some value, but I will rely on you to suggest other improvements and bug fixes.  Please use the CodePlex MvpXml Discussion page to leave comments/suggestions/bug reports, and if you are interested in helping with the project, well, we would love the help.

Published Friday, July 07, 2006 10:46 AM by donxml
Filed under: , ,

Comments

John Workman said:

This is great. This is one of those additions that makes you ask, "why wasn't this there in the first place".

I also appreciate you opening my eyes to extending VS. Adding tools like this will save countless hours.
Thanks!
John
July 10, 2006 8:20 AM

Microsoft XML Team's WebLog said:

manual trackback
July 11, 2006 7:24 AM

CoqBlog said:

manual trackback
July 11, 2006 7:25 AM

Noah Coad's Code said:

manual trackback
July 11, 2006 7:27 AM

John Workman said:

manual trackback
July 11, 2006 7:28 AM

Nick Parker said:

Nice work Don, recently I created an XPath Debugging Visualizer for Visual Studio 2005. If your interested, check it out:

http://developernotes.com/archive/2006/04/26/833.aspx
July 11, 2006 12:00 PM

Dean Harding said:

I have a question... I've got VS.NET 2005 Professional, but when I install XPathmania, I can't see it in my View->Other Windows menu item... do know why that might be? I can see the addin is installed when I go to Help->About.
July 11, 2006 9:32 PM

Don Demsak said:

Any installation issues, please either contact me via my contact page http://www.donxml.com/GrokThis/contact.aspx, or better yet, use the XPathmania discussion forums on CodePlex: http://www.codeplex.com/Project/ListThreads.aspx?ProjectName=MVPXML&ForumId=209
July 11, 2006 11:31 PM

pleasantday said:

Ok, but how would this work for queries against xml file using attributes for searching?

<mydata>
<myrec id="01" FavColor="Brown" />
<myrec id="02" FavColor="Red" />
<myrec id="03" FavColor="Green" />
</mydata>

Suppose I just want the record for id="02"
July 18, 2006 1:26 PM

Tim Walters said:

Like all XPath queries you could use the '@' symbol to specify the attribute axis, there's also a long-hand version if you're interested. Not something I'll confuse you with right now.

/mydata/myrec[ @id = '02' ]

That would get the record you're after.
July 21, 2006 12:57 AM

Peter Bromberg said:

Nice work DonXML!
July 22, 2006 10:03 PM

Travis Laborde said:

What would be really cool is the opposite as well - highlight a node and have the tool create an XPath query that would return that node.

Of course it should/would also show other nodes that get retured for that query as well.

But nice work! Great time saver!
August 7, 2006 3:22 PM

Christopher Steen said:

Pervasive Tools, a couple more tools [Via: James Avery ] VS 2005 trick for obtaining the public key...

September 19, 2006 9:18 PM

Marks Remarks said:

I saw these noted in some blog readings in the past few days... they might help with those trying to use XPath with XML data : XPathmania -- http://donxml.com/allthingstechie/archive/2006/07/07/279 ...

September 26, 2006 1:18 PM

mike said:

Great work.  Stumbled across this site looking for a utility that did this . . .  VS come so close but just misses it on key things like this.

September 29, 2006 2:17 PM

JrzyShr Dev Guy said:

If you spend a lot of time working with XPath, you probably know that it's not as easy as it could be

October 12, 2006 10:25 AM

TrackBack said:

Microsoft Visual Studio Extensibility Contest Winners
October 23, 2006 5:03 AM

Mugun said:

Really awesome tool to save countless time. Thanks
June 6, 2007 12:37 AM

Finds of the Week - December 8, 2007 » Chinh Do said:

December 8, 2007 2:51 PM

Eric Stott said:

Is there a way to use XPath functions in the queries? http://www.w3schools.com/xpath/xpath_functions.asp Thanks, Eric
January 7, 2008 10:02 PM

Matthew Hazzard said:

First, I absolutely love this plug-in.  It transforms VS2005 to one of the best XML editing/debug tools available.  I haven't even bothered to renew my license for other xml tools.

However, I recently began using VS 2008.   VS2008 seems to do a much better job of formatting very large documents.  Unfortuneatly, the plug-in will no longer function in 2008.   I even made an attempt to rebuild it and tweak the installer for 2008.  After fiddling with it for a bit (I was able to load it) I realized it was going to take more time than I can afford to spend at the moment to make it functional.   Is anyone working on porting this to the VS2008 plug-in model?  

Thanks a ton!

January 28, 2008 9:48 AM

Arthur Nesterovsky said:

Very useful and simple in use tool. Thanks a lot.
January 29, 2008 3:48 AM

donxml said:

Matthew Hazzard - I haven't had the opportunity to upgrade the code to use the new VS 2008 edtior.  I get so little feedback from folks that I just assume that no one is really using it, so upgrading to VS 2008 has been low on my priority list.  But I'm glad to see that at least someone is using it, and even tried to crack open the code ;)

January 29, 2008 4:44 AM

DM said:

Sorry for being late... I use your add-in and like it a lot... Nice work. I would really appreciate it a lot if you could update it to work inside VS 2008. I'm slowly switching from VS 2005 to VS 2008 so an update on this tool would be really great news! Thanks in advance!
February 12, 2008 6:39 PM

Matthew Curland said:

The 2008 issue comes down to the use of classes from the Microsoft.VisualStudio.Package.LanguageService assembly. The xml editor in VS2008 is based on classes on the 9.0 version of this Dll (tack ".9.0" onto the end of the assembly name), which do not inherit from the 8.0 MPF (Managed Package Framework) classes XPathMania is compiled against. You might just be able to change the reference and recompile for VS2008, but you will not get one dll to work in both environments with this approach. An alternate approach is to drop the use of the MPF classes used to get the IVsTextLines and use the interfaces directly. The main issue with this approach is that you need to do your own change tracking (you lose the Source.ChangeCount property). I originally took the same MPF approach in the 'PLiX Snippet Preview Window' implementation (PLiX=Programming Language in XML, an XML based code generator), but it did not work in VS2008 because of the MPF version issue. (As of this writing, I haven't posted a new PLiX setup. e-mail me at mcurland at the university if you want one). You can see the change log at http://plix.svn.sourceforge.net/viewvc/plix/CodeGenCustomTool/SnippetPreviewWindow.cs?r1=66&r2=68 I'd like to jump in on the thanks to Don for the tool. I've used it extensively in an XML class I teach.
February 15, 2008 9:28 AM

David Pepper said:

I'm alarmed to hear you get little feedback. I think this is an excellent product, simple but very useful - it is well used within my team. If you do get the chance to port it to VS2008 that would be great.
February 20, 2008 3:40 AM

Matthew Curland said:

I wanted one for VS2008, but didn't have time to attempt to make it work with one Dll. I know this isn't the official way to post, but you can find a preliminary VS2008 install (installs as a separate application) at http://www.PowerVB.com/XPathMania/Mvp.Xml_XPathMania_1.0RC1.1VS2008.zip The changes here are limited to new project and solution files for 2008, installer tweaks for a different product, and some conditional complication in 2 code files. Don, e-mail me at the university (mcurland) if you want the source. I'll leave it up to someone else to do single-file exercise I mentioned earlier. Please let me know if you have any issues with it. -Matt
February 20, 2008 2:57 PM

Matthew Hazzard said:

Thanks a ton for doint the port Matt C. -- U R a rockstar. This plug in is just so useful. -- Matt H.
March 17, 2008 7:57 AM

David Turkel said:

I second the thanks to Matt C. Well done! -- Dave T.
April 27, 2008 4:30 PM

Mark Snyder said:

Thanks so much for this awesome tool, guys! I'm one of probably a great many lurkers to this site. The product developed by the company where I work employs XML heavily and XPathMania is a godsend to help diagnose problems. If and until MS integrates some similar feature into the editor, you should never feel like people aren't using this. It's great to be able to quickly and seamlessly, from within VS, to develop and test XPaths. When I'm using VS2008 and need to do this kind of thing, I run up VS2005 just so I can get XPathMania. I haven't tried the VS2008 version yet, but that's what I was here to find, so thanks in advance.
May 7, 2008 11:50 AM

Anthony Main said:

I've tried the 2008 release Matt but I get an error saying filename wasn't specified and it rollsback :( NEEEEED 2008 version.
July 9, 2008 8:54 AM

Rickard Nilsson said:

This is a great tool, thanks Don and Matthew C for porting it. Works great with Visual Studio 2008!
August 18, 2008 4:47 AM

Ans Vlug said:

Thank you for this great tool! It's not that I didn't want to use it, I've just been looking for it a long time ;-)
September 16, 2008 4:32 AM

Roland Sanidad said:

Thank you for this excellent and well used tool Don. One of the reasons I kept an instance of VS 2005 was for this tool. Thanks to Matt C. for the 2008 update.
December 3, 2008 2:09 PM

Ricardo Guerra said:

thanks Don and Matt! great work!
January 9, 2009 9:32 AM

Rasmus Jakobsen said:

PLEASE do a VS 2008 tool also!
April 20, 2009 8:01 AM

MindaugasL said:

Thanks Matthew Curland for VS2008 build. Successfully installed :)
May 13, 2009 1:57 PM

Roger Bruvold said:

I agree this is a great tool, and I'm glad it's available for 2008. Some coincidence, it was Mathew Hazzard who turned me on to it in the first place. How's it going Matt? Roger B.
June 12, 2009 10:50 AM

Alex said:

Greate tool! Absolutlely essential. Also used to inofficial link from Matthew Curland. Thanks Matthew for providing the 2k8 version!
October 9, 2009 4:38 AM

Pete Story said:

How can anybody think this is not used! It's great. I'm just moving from VS2005 (really) to VS2010. Is it possible to have XPathMania there too?
February 18, 2011 8:11 AM

morteng said:

So what about an update for Visual Studio 2010?? Need it so much :|
March 31, 2011 3:29 AM

Mike Cline said:

Been a while since anyone commented on this thread, so I just thought I would add in my two cents and say that this tool is invaluable and I don't want to see it die. thanks Matt Curland for providing the updated VS2008 version! I love you. Are there other tools available (outside visual studio) that provide the same functionality as this? If so I haven't been able to find them. I find it hard to believe that more and more people aren't needing a tool like this!
April 27, 2011 2:32 PM
Anonymous comments are disabled

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems