![]() |
Languages »
XML »
General
Intermediate
Advanced XPath AnalyzerBy ManOwaRAn advanced XPath Query Analyzer, based on the work of Enrico Elizar Samuel. This advanced version supports namespaces resolution. |
C#, XML, Windows, .NET 1.1, ASP.NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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
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:
XmlNamespaceManager with all the needed namespaces
XPathExpression that contains the relative XPath
Expression
XmlNamespaceManager object to that
XPathExpressionThat, in pratice, means to write something like:
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.
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!
http://localhost/nwind/schema/schema1.xsd/Employees?root=root
//root/Employees
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Dec 2003 Editor: Nishant Sivakumar |
Copyright 2003 by ManOwaR Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |