473,498 Members | 523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you get the XPath expression of the current node?

Rob
Hi,

I am moving through an XML document using an XPath Navigator, and I'd
like to be able to get the xpath expression for the location of the
current node from the root node.

Any ideas how to do this? I was hoping for a property of the navigator
object, but I can't find it.....

Thanks in advance for any help!
Rob
Code sample:
private void Page_Load(object sender, System.EventArgs e)
{
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument(@"c:\template.xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
nav.MoveToFirstChild();

do
{
if (nav.NodeType == XPathNodeType.Element)
{
if (nav.HasChildren)
{
nav.MoveToFirstChild();
//what is the XPath expression for this node?
}
}
} while (nav.MoveToNext());
}
Nov 12 '05 #1
1 10915
Rob wrote:
I am moving through an XML document using an XPath Navigator, and I'd like
to be able to get the xpath expression for the location of the current
node from the root node.


I'm not aware of any toolset that provides that functionality, .NET
Framework or otherwise. I think that your question is under-specified: for
any given node in an XML instance document, there may be a very large number
of XPath expressions that all describe the node.

Consider the <c> element in this document:

<a>
<b/>
<b><c/></b>
<b><d/></b>
</a>

Here are a just few of the XPaths that describe it:

/a/b/c, //a/b/c, //b/c, //c, a/b/c[1], /a[1]/b[2]/child:*,
/a/b[d]/preceding-sibling::*/c, //d/ancestor::*[2]//c, /*[1]/*[2]/*[1], etc.

Some of these seem contrived, but each expresses a different relationship
among the nodes. The different relationships could indicate how an
XPathNavigator navigated to particular node.

So, restating the issue, there is no "the" XPath expression for the location
of a node.

When I had a similar need a while back, I created an XmlReader that that
kept track of the currently nested elements and position counts, and exposed
that through a property, so the "canonical" XPath in my custom XmlReader
would look like

/a[1]/b[2]/c[1]

It was not overly complex. This might help you get started.

public class XPathXmlReader : XmlReader
{
private XmlReader reader;

// this ctor allows XmlReaders to be chained
public XPathXmlReader( XmlReader reader ) { this.reader = reader; }

private Stack currentPath;
public string CurrentPath { get { /* ... */ } }

/*
implement all the XmlReader methods in terms of this.reader
and use the currentPath stack to keep track of where you are.
*/
}

Exposing a CurrentPath property for an XPathNavigator would require a
different approach. If performance expense were no object, you could wrap an
XPathNavigator inside of a custom XPathNavigator, and have the custom class
use a combination of methods like MoveToParent and MoveToPrevious (etc.) to
determine the path back to the root from the current node. you could do that
either on a Clone of the object, or navigate back to the current node
position. Obviously, neither of these scale well to large XML documents. You
might also leverage the SelectAncestors method.

Cheers,
Stuart Celarier, Fern Creek
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2884
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
1
5442
by: kurt hansen | last post by:
hi I thought that this would be easy, but maybe not so much. I want to: pass an xpath expression and a string value to a stylesheet and copy the source xml document, changing the value of...
3
1722
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't...
6
15479
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
13
3213
by: David Thielen | last post by:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software" nav.SelectSingleNode"."); The select returns an exception: + $exception {"'.' has an invalid token."} System.Exception...
3
6769
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
1
2388
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
7
2789
by: Tim Hallwyl | last post by:
Hi, there! As I understand the XPaht recommendation, the context node is a node; not a node-list, not XPath object -- but a single node. Now, the WS-BPEL 2.0 specification allows an XML simple...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.