SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

XPath Queries

SharpDevelop 2.1 now has an XPath Query window where you can run an XPath query on the active XML document and the matched nodes will be highlighted.

Features

  • Shows a list of XML nodes matched by the XPath query.
  • Highlights the matched XML nodes in the XML Editor.
  • Namespaces supported.

Running an XPath Query

Open an XML document in SharpDevelop. For this walkthrough I am going to use the following WiX document.

<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2003/01/wi">
    <Product Id="21BF5D59-D3AD-46FC-97B2-A3DD23A5BE20"
        Name="Your Application"
        Language="1033"
        Version="1.0.0.0"
        Manufacturer="Your Company">
 
        <Package Id="8874187C-94B0-4DB4-9B73-E4D635680BA3"
            Description="#Description"
            Comments="Comments"
            InstallerVersion="200"
            Compressed="yes"/>
  
            <Property Id="AcceptLicense">No</Property>
            <Property Id="Button_Back">&lt; &amp;Back</Property>
            <Property Id="Button_Next">&amp;Next &gt;</Property>
            <Property Id="Button_Finish">&amp;Finish</Property>
            <Property Id="Button_OK">OK</Property>
            <Property Id="Button_Cancel">Cancel</Property> 
            <Property Id="Button_Exit">&amp;Exit</Property>
     </Product>
</Wix>

From the View menu, select Tools and then select XPath Query.

XPath Query menu item

This opens the XPath Query window.

XPath Query Window

If your XML document has any namespaces that you need to refer to in the XPath query you can add them to the Namespaces tab. In the image below the WiX namespace has been entered along with a prefix.

WiX namespace added to XPath Query namespaces

The prefix cannot be an empty string even for an XML document that does not use explicitly use a namespace prefix (e.g. like the WiX document we are currently using).

Type in your query into the XPath text box and then either click the Query button or press the return key to execute the query and show the results. The results are displayed as a list in the XPath Query window and the corresponding nodes in the XML document are highlighted.

XPath query results

Double clicking a row in the Results list will jump to the corresponding XML line and sets focus back to the XML Editor. Single clicking a row will cause the XML Editor to scroll to the matched node.

If the XPath you typed in has an error this information is displayed in the Results list when you try to execute the query.

XPath query error

Any errors in the XML document are also displayed in the Results list when you attempt to execute the query.

Xml document errors

To remove the highlighting in the XML document, right click the document and select Hide XPath Results.

Hide XPath results menu item

Visual Studio

If you want to do same thing in Visual Studio 2005 then take a look at XPathmania created by Don Demsak.

Published Aug 05 2006, 02:44 PM by MattWard
Filed under:

Comments

 

l33ts0n said:

Awesome feature, Matt!

However, I'm a little bit confused by why you guys chose to put "Hide XPath results" in the right-click text buffer? It's not very discoverable.

If I can propose a suggestion to the functionality, I would recommend adding a checkmark labeled "Highlight results in text buffer" to the XPath Query window; it seems a lot more natural for the option to be there than in the buffer context menu, personally.

What do you think?
August 6, 2006 3:44 AM
 

MattWard said:

Thanks for the feedback. The reason it was added it to the right click menu (its also available on the Edit menu) is because I have had the opposite problem with the Code Coverage results window where the Show/Hide Code Coverage button is not easy to find. With code coverage you typically don't need the Code Coverage results window open so it is more of an issue. Another reason is that typically you'll be editing the XML when you decide that the highlighting is annoying you rather than from the query window itself. This being said, I think a way of hiding the results from the XPath Query window makes sense since you need this open whilst running the queries.

The next question is how should it work? Your suggestion is to have some sort of toggle. If a toggle is used then the user would have to explicitly check/uncheck the highlighting. At the moment the nodes are always highlighted when a query is run and the Hide XPath results menu option can be used to remove the highlighting, it does not permanently turn off the highlighting.
August 6, 2006 3:13 PM
 

l33ts0n said:

You make some good points I hadn't considered.

Maybe include a toggle, but by default have it set to show highlighting in the editor? I can see that for some people constantly having to disable the highlighting each time they do an XPath query might be undesirable.

Ahh. Maybe when (if?) #develop migrates to WPF, most of this will be moot: you'd just have an overlay of mini-controls that were on, in, or around the editor window itself that would give quick access to this kind of functionality without having to search through a context menu or bring up an addin pad. :)
August 6, 2006 7:41 PM
 

DonXml's All Things Techie said:

August 6, 2006 9:53 PM
 

MattWard said:

Another thing is that I do not want to overload the user interface with lots of extra check boxes and buttons that are infrequently used. If we add a toolbar button, like the Code Coverage window, then that is a line wasted, and the list view is pushed down. If we add a check box to the window, say on the same line as the query text box, then this can be tabbed to, but is it an option that needs to be tabbed to every time? I would not think so.

I see two general modes of working:

1) User never wants to see the xml highlighted and is satisfied with the results being displayed in the list view.
2) User wants to see the xml highlighted, but when they start to edit the XML they want a way to remove the highlighting.

Presumably not many people would fall into mode 1), but this could be satisfied with a checked menu or even an option in the Tools/Options window. With mode 2) a right click menu option, not checked, is good enough. At the moment the XPath Query window is written for mode 2).

Another thing that might be useful is to extend SharpDevelop to support more than one toolbar like Visual Studio does. Then we could have an Xml Query toolbar button that shows/hides the XPath results instead of putting the button in the window itself.

Looking at some other pure XML editing applications such as:

oXygen - http://www.oxygenxml.com/xpath.html
Stylus Studio - http://www.stylusstudio.com/xpath_evaluator.html
XML Spy - http://www.xmlspy.com

They tend not to highlight all matching nodes, but only the one selected in the results list. Not sure whether I prefer highlighting just the selected node or all the nodes, probably the latter. I guess we could mark the currently selected node in a different colour to differentiate it.

One nice feature that XmlSpy has is real-time evaluation where the results are updated as you type in the XPath query without needing to hit the Query button. However they have an option to turn this off and I can see people might not like the results list changing whilst they are correcting an xpath.

I did briefly try turning off the highlighting when the XML editor regains focus, but this didn't really work very well since you could not use the editor's scrollbar without losing the highlighting. Hence the need for a way for the user to turn the highlighting off.
August 6, 2006 9:57 PM
 

Rupepa said:

Matt,

Congratulations! Nice tool! A agree with you. The way you implemented XPath Query is clean, simple and also intuitive! I like the "Hide XPath Results" command in context menu. Perhaps you could extend the Standard Toolbar with only one more tool (icon) with "Show/Hide" XPath highlight (or check/uncheck tool).

Matt, don't you think that we could have more than 5 recent queries in dropdown? Perhaps 8 or 10?
I'm not sure (I'm new in Xpath - this is the first time a use XPath), but I think that using the same prefix for different namespaces doesn't make sense. I'm I wrong? If I'm not wrong, it would be a good idea, send a message to the user, if he tries to use a prefix that already exist.

There is one more comment: Don't you think that having some more information about the nodes in XPath Query window, would be useful to identify the node? I mean something like its value, and/or its attributes (all in one line in 3rd column or as a tooltip).

Of course, all this topics are not so important! The important thing is that now we have one more nice tool in SharpDevelop, for us work with!

Thank you!
August 7, 2006 10:34 PM
 

jaredcooper said:

Matt,

Although I haven't tried this yet, I can see from the screen-shots and feedback that this is an excellent tool.  I expect I'll be making a lot of use of this.

I'd also like to make a suggestion - To me, it would make a lot of sense to make the Find window a bit more generic, in a similar way to view-contexts (maybe it already is and I've not noticed).

This way different file types can have different find engines registered.  For most files, this will only have the default Find window, but for XML a tabbed-window could show the default find, plus an XPATH tab.

A side-effect of this would be that both highlighting the current found match or using Find All to highlight all matches could be done from the existing Find/Find All buttons, allowing the developer to choose the highlighting style thet prefer.

Just a thought.

Jared
August 8, 2006 12:13 PM
 

MattWard said:

Rupepa,

SharpDevelop's main toolbar has too many icons at the moment, but I think if SharpDevelop is changed to support configurable toolbars then the Show/Hide XPath Results check box can go in its own XPath Query toolbar. Maybe with its own text box for XPath queries too.

The limit of 5 queries in the history is my mistake, I was testing the code and artificially set the limit to 5 to make sure the history list was working correctly, and forgot to change it back. Originally it was a large number, something crazy like 30. I'll update the repository code to fix this.

Yes, using the same prefix for different namespaces does not make sense, since how do we know which namespace it refers to? I think the Namespaces grid should show an error for the row if the namespace prefix already exists.

The highlighting in the XML editor is supposed to identify the node. At the moment it does not highlight the currently selected node differently, which I'll change. As for extra information in the list view, Stylus Studio has two columns, name and value, where the value seems to be the inner text of the matched node. We could do that. For text nodes the name and value column's would be the same. I actually thought about putting the node's XML in the tooltip, but I'm not sure I like this idea.

Jared,

Using the Find window is an interesting idea. The first thing that I thought was that the Find window has  different search types in the Use dropdown. The only problem here is that there's no way to enter namespace information. I would also need to implement an XPath replace, just for completeness. Adding a XPath tab to the Find window is a possibility, however no other XML editing application that I know of overloads the Find window in this way. At the moment the Find window is only for text searches. If it naturally fitted into the Search/Replace tab then it would be a definite possiblity. I also prefer using a docked window rather than the modeless Find window.
August 8, 2006 6:46 PM
 

jaredcooper said:

Matt,

Fair points.  Extending the Find window is probably a little over the top for one additional search type.  Equally, for a search like XPATH, where the search criteria are likely to be updated frequently, rather than an occaisonal Find, having the XPATH window docked makes more sense than a modal window.

Maybe there'll be another file type introduced that has requirement for a different search that makes it more appropriate then to extend Find, but for now a seperate XPATH search makes perfect sense.

Again, another excellent tool that will get a lot of use.

Jared
August 8, 2006 8:42 PM
 

Rupepa said:

Matt,

I was testing your new implementation "Go To XML Schema Definition" and I ended up opening the 'data\schema\XMLSchema.xsd' file. When I tried to use XPath Query, SharpDevelop( Version 2.1.0.1683) showed the Unhandled exception window with a message: ...could not find XMLSchema.dtd file....  Perhaps you want to handle this error! By the way, where you prefer to receive this kind of post (bug report specific and related to your new tools)? Here (blog) or at the forum?

Thank you!
August 10, 2006 12:44 AM
 

MattWard said:

Thanks for the bug report. Generally I set the XmlTextReader.XmlResolver to null to stop this error from occurring. You found some code that was not doing this. A fix should be committed at some point this evening.

You can post bug reports to the bug reporting forum, where the bug is potentially more visible than a comment and should be looked at by more than just me, however I don't really mind where the issue gets posted.

http://community.sharpdevelop.net/forums/19/ShowForum.aspx
August 10, 2006 12:44 PM
 

DonXml's All Things Techie said:

If imitation is the sincerest form of flattery, well then I&#39;m very flattered. Matt Ward announced

October 13, 2006 6:43 PM
 

Matt Ward said:

Here is the list of features that have been added to SharpDevelop 2.1. Feature Description Code Analysis

March 5, 2007 7:29 PM
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.