Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET
Article

Advanced XPath Analyzer

Rate me:
Please Sign up or sign in to vote.
4.07/5 (10 votes)
30 Dec 20032 min read 113K   1.3K   25   16
An advanced XPath Query Analyzer, based on the work of Enrico Elizar Samuel. This advanced version supports namespaces resolution.

Introduction

This is an advanced version of the good XPath Analyzer done by Enrico Elizar Samuel (http://www.codeproject.com/aspnet/xpathanalyzer.asp). 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
<?xml version="1.0" encoding="utf-8"?>
<items link="~/Html/Default.htm" xmlns=http://site/menu 
    xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 
    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>
    <item title="Services" link="#">
        <item title="Service 1" link="#" />
        <item title="Service 2" link="#" />
    </item>
    <item title="People" link="#"/>
    <item title="Opening Hours" link="#"/>
    <item title="Prices" link="#"/>
    <item title="Contacts" link="#"/>
</items>

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:

/items/item[@title='Products']

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:

/def:items/def:item[@title='Products']

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:

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

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:

http://localhost/nwind/schema/schema1.xsd/Employees?root=root

XPath query:

//root/Employees

History

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

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


Written By
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.

Comments and Discussions

 
QuestionWhat's advanced? Pin
Stephane Rodriguez.17-Dec-03 1:23
Stephane Rodriguez.17-Dec-03 1:23 
AnswerRe: What's advanced? Pin
manowar17-Dec-03 2:23
manowar17-Dec-03 2:23 
GeneralRe: What's advanced? Pin
Uwe Keim17-Dec-03 22:06
sitebuilderUwe Keim17-Dec-03 22:06 
GeneralRe: What's advanced? Pin
manowar17-Dec-03 22:15
manowar17-Dec-03 22:15 
GeneralRe: What's advanced? Pin
Daniel Cazzulino [XML MVP]22-Dec-03 14:41
Daniel Cazzulino [XML MVP]22-Dec-03 14:41 
GeneralRe: What's advanced? Pin
manowar22-Dec-03 20:48
manowar22-Dec-03 20:48 
GeneralRe: What's advanced? Pin
Daniel Cazzulino [XML MVP]22-Dec-03 23:59
Daniel Cazzulino [XML MVP]22-Dec-03 23:59 
GeneralRe: What's advanced? Pin
manowar23-Dec-03 7:59
manowar23-Dec-03 7:59 
GeneralRe: What's advanced? Pin
Daniel Cazzulino [XML MVP]23-Dec-03 9:41
Daniel Cazzulino [XML MVP]23-Dec-03 9:41 
GeneralRe: What's advanced? Pin
manowar26-Dec-03 0:02
manowar26-Dec-03 0:02 
Well, at the end we found ourself on the same side of the river. I'm happy about that! I know that i haven't explained deeply the changes i've done to the original software, but, belive me, is just because i haven't got the time to do it. I've found the original XPath Analyzer, saw that it could be useful to me with some little changes (the namespace resolution above all) and, once done the modification i needed, i decided to realease it to the community so that it may be of help to other people having the same needings. I've found that what we may consider unuseful or worth nothing, can be really useful to other man in a way that we can't imagine!

Btw, i've seen your blog and added it to my Rss Aggregator Smile | :)

Do you like Manowar too? I really *love* their music! I've been to all concerts they've done here in Italy Smile | :) ..it's incredible how they still be the kings of metal! (You know, they're no more so young Big Grin | :-D )

Have a happy New Year!



-=ManOwaR=-


AnswerRe: What's advanced? Pin
Charles E. Wagner Jr.23-Dec-03 10:50
Charles E. Wagner Jr.23-Dec-03 10:50 
GeneralRe: What's advanced? Pin
Daniel Cazzulino [XML MVP]6-Jan-04 5:35
Daniel Cazzulino [XML MVP]6-Jan-04 5: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.