Click here to Skip to main content
12,629,513 members (36,806 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked

Advanced XPath Analyzer

, 30 Dec 2003
Rate this:
Please Sign up or sign in to vote.
An advanced XPath Query Analyzer, based on the work of Enrico Elizar Samuel. This advanced version supports namespaces resolution.


This is an advanced version of the good XPath Analyzer done by Enrico Elizar Samuel ( Advanced XPath Analyzer has been re-written in C# and supports namespaces resolution and navigation.

Notes about the code

The main difference from the original XPath Analyzer is that you can navigate into an XML document using qualified names. It is also possibile to have a default namespace in the XML document. In this case a the namespace prefix will be def, so that you can refer to it in your xpath expression.

Let's say that, for example, we have an XML document that looks like this one:

<?xml version="1.0" encoding="utf-8"?>
<items link="~/Html/Default.htm" xmlns=http://site/menu 
    xsi:schemalocation="http://site/menu menu.xsd">
    <item title="Products" link="#">
        <item title="Product 1" link="#"/>
        <item title="Product 2" link="#"/>
        <item title="Product 3" link="#"/>
        <item title="Product 4" link= "#"/>
    <item title="Services" link="#">
        <item title="Service 1" link="#" />
        <item title="Service 2" link="#" />
    <item title="People" link="#"/>
    <item title="Opening Hours" link="#"/>
    <item title="Prices" link="#"/>
    <item title="Contacts" link="#"/>

That is, as you may have guessed, the navigation menu of a simple website. If you need to get the Product 3 link, you should write an XPath query like:


but this query won't give any result! This because you need to specify that the elements items and item belongs to the default namespace http://site/menu as it really is, otherwise the parser will understand that they aren't associated with any namespace.
So, the correct query is:


in order to run correctly this query you first need to define that the prefix def points to the default namespace URI. With the Advanced XPath Analyer you can just manually add the namespace or ask to it to detect it automatically, but what really happens behind the scenes?
Three steps are needed to make the XPath query run without errors:

  1. Define an XmlNamespaceManager with all the needed namespaces
  2. Create an XPathExpression that contains the relative XPath Expression
  3. Assign the created XmlNamespaceManager object to that XPathExpression

That, in pratice, means to write something like:

XmlNamespaceManager namespaceManager = 
    new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace(prefix, uri);
XPathExpression expression = navigator.Compile(xpathQuery);

Where prefix and uri are the variables that contains the relative values, and navigator is the XPathNavigator that you're using to navigate through the document.

Using the program

You have to specify the XML source file (which can be a file on your hard disk or a file somewhere in the web) and then the XPath Query you'd like to test. You can also specify a SQLXML url so that you can test your SQLXML Queries. If your XML uses namespaces you have to add it in the left panel so that you can use them in your XPath Query, otherwise you can detect it automatically, clickin' on the Detect Namespace button. Of course remember to do this before you analyze the XPath Query!

Location of XML document:


XPath query:



  • v 1.0: First version
  • v 1.1: Added automatic namespace discovery


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

Web Developer
Italy Italy
Born in 1977, works as a freelancer, focusing on Database / Software Architecture.
In the free time writes articles for two of the major Italian programming magazines (Computer Programming and VBJ) and also develops nice and useful programs.
His major interests and skills are the .NET framework (C# in particular) and SQL Server.

You may also be interested in...


Comments and Discussions

QuestionWhat's advanced? Pin
Stephane Rodriguez.17-Dec-03 2:23
memberStephane Rodriguez.17-Dec-03 2:23 
AnswerRe: What's advanced? Pin
ManOwaR17-Dec-03 3:23
memberManOwaR17-Dec-03 3:23 
GeneralRe: What's advanced? Pin
Uwe Keim17-Dec-03 23:06
sitebuilderUwe Keim17-Dec-03 23:06 
GeneralRe: What's advanced? Pin
ManOwaR17-Dec-03 23:15
memberManOwaR17-Dec-03 23:15 
GeneralRe: What's advanced? Pin
Daniel Cazzulino22-Dec-03 15:41
memberDaniel Cazzulino22-Dec-03 15:41 
GeneralRe: What's advanced? Pin
ManOwaR22-Dec-03 21:48
memberManOwaR22-Dec-03 21:48 
GeneralRe: What's advanced? Pin
Daniel Cazzulino23-Dec-03 0:59
memberDaniel Cazzulino23-Dec-03 0:59 
GeneralRe: What's advanced? Pin
ManOwaR23-Dec-03 8:59
memberManOwaR23-Dec-03 8:59 
GeneralRe: What's advanced? Pin
Daniel Cazzulino23-Dec-03 10:41
memberDaniel Cazzulino23-Dec-03 10:41 
I've to admit it, you have a point regarding the general goal of CodeProject. And you're also right about simply ignoring content that doesn't appeal to me.
I didn't mean to be rude, I apologize for that. The reason for my reaction is that I think there isn't any fun or teaching in "forking" someone else's article, making a new one that doesn't even bother to explain anything and doesn't even reach 1 page of text, calls itself "advanced" and only adds marginal (what? 10 lines of code? you didn't even bother to explain that!) value to the original.
This is SO contrary to the principles of community and opensource that it touched me, nothing more. In an OS-friendly way, you would have discussed your ideas on how to advance the example, and contribute your extensions to him.
About my site, it's on a dynamic IP, and from time to time, it fails to syncronize and someone else's site is failing (I guess). That's why I send people to the SourceForge site, instead of mine.
And any of those subprojects have nothing to do with web applications, so "my" site failing isn't a proof of anything. But's that another issue.
I'd just like you to think about what does learning, teaching and having fun means.

Anyways, Manowar rocks (the latest album is certainly a strange one, but what the hell, they can do whatever they want at this point! they still are the kings of metal Smile | :) ).
I do wish you a nice xmas. If I started this thread sort of violent, I'm sorry.

Daniel Cazzulino
My Weblog

Coauthor of:
Beg. C# Web Apps
ASP.NET Components Toolkit
Beg. Web Programming w/VB.NET & VS.NET
Pro ASP.NET Server Contro
GeneralRe: What's advanced? Pin
ManOwaR26-Dec-03 1:02
memberManOwaR26-Dec-03 1:02 
AnswerRe: What's advanced? Pin
Charles E. Wagner Jr.23-Dec-03 11:50
memberCharles E. Wagner Jr.23-Dec-03 11:50 
GeneralRe: What's advanced? Pin
Daniel Cazzulino6-Jan-04 6:35
memberDaniel Cazzulino6-Jan-04 6:35 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161205.3 | Last Updated 31 Dec 2003
Article Copyright 2003 by manowar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid