472,801 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,801 software developers and data experts.

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


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!
Code sample:
private void Page_Load(object sender, System.EventArgs e)
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument(@"c:\template.xml");
nav = docNav.CreateNavigator();

if (nav.NodeType == XPathNodeType.Element)
if (nav.HasChildren)
//what is the XPath expression for this node?
} while (nav.MoveToNext());
Nov 12 '05 #1
1 10769
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:


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


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.

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

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...
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...
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...
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>
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...
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>...
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...
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...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.