473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XPathNavigator.getUniquePath()

Hi;

Is there a way from an XPathNavigator object to get an xpath string that
will, when used in a Select(xpath) on the underlying base/root XPathNavigator
return the same XPathNavigator?

In other words, I initially create an XPathNavigator for my entire xml
document. To get an XPathNavigator object who's root is a given node in the
original xml document, there is a unique xpath that will return that node.
The unique xpath could have, as an example, [2] in it depending on which node
in a list of identical nodes, this one is.

I may do a Select on a Select on a Select and have an XPathNavigator. But I
want to later be able to get that same XPathNavigator by doing a single
select on my original root XPathNavigator.

Can this be done?

--
thanks - dave
Nov 12 '05 #1
7 4058
Hi dave,

Currently we are looking for someone to help you on this issue, we will
reply to you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 12 '05 #2
David Thielen wrote:
I may do a Select on a Select on a Select and have an XPathNavigator. But I
want to later be able to get that same XPathNavigator by doing a single
select on my original root XPathNavigator.


Why don't you just clone and save current XPathNavigator?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #3
Hi;

I'm porting some java code over (that uses dom4j) and it includes the
ability to build up a new xpath using the getUniquePath() from another select
as part of it. I can change the code to use the navigator objects but boy
will it be a PITA.

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:
I may do a Select on a Select on a Select and have an XPathNavigator. But I
want to later be able to get that same XPathNavigator by doing a single
select on my original root XPathNavigator.


Why don't you just clone and save current XPathNavigator?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #4
There is no equivalent to getUniquePath() in the .NET XmlDocument object
model, but the docs on this method seem to indicate it does a very
simplistic calculation which is pretty easy to simulate as follows:

class NodeLocator {
private NodeLocator next;
private XmlNode node;

private NodeLocator(XmlNode node) {
this.node = node;
}

public static string GetUniqueLocation(XmlNode node) {
NodeLocator loc = new NodeLocator(node);
while (node.ParentNode != null ) {
node = node.ParentNode;
if (node is XmlDocument) break;
NodeLocator parentloc = new NodeLocator(node);
parentloc.next = loc;
loc = parentloc;
}
return loc.ToString();
}

public override string ToString() {
StringBuilder sb = new StringBuilder();
NodeLocator loc = this;
do {
sb.Append('/');
sb.Append(loc.node.Name);
sb.Append('[');
sb.Append(loc.IndexInParent);
sb.Append(']');
loc = loc.next;
} while (loc != null);
return sb.ToString();
}

private int IndexInParent {
get {
int indexInParent = 0;
XmlNode parent = node.ParentNode;
if (parent != null) {
foreach (XmlNode child in parent.ChildNodes) {
if (child == node) break;
if (child.Name == node.Name) {
indexInParent++;
}
}
}
return indexInParent;
}
}
}

This produces a path like "/x:foo[0]/x:item[1]". But this is a very
fragile XPath - any edits in the tree could break it since the hard coded
indexes [0] and [1] and so on, could now be wrong. A better XPath
expression would take into account the presence of unique ID's in the DOM
tree and use those as anchor points so that the resulting XPath location is
more robust in the face of subsequent DOM tree edits. So you would get
paths like: "/x:foo/x:item[@id='abc']". Now this XPath will work even if
you insert new items before this one, assuming the id's are still unique.
This could be done using the SchemaInfo provided on the XmlNodes and is left
as an exercise for the reader :-)

Chris.

"David Thielen" <th*****@nospam.nospam> wrote in message
news:B2**********************************@microsof t.com...
Hi;

I'm porting some java code over (that uses dom4j) and it includes the
ability to build up a new xpath using the getUniquePath() from another
select
as part of it. I can change the code to use the navigator objects but boy
will it be a PITA.

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:
> I may do a Select on a Select on a Select and have an XPathNavigator.
> But I
> want to later be able to get that same XPathNavigator by doing a single
> select on my original root XPathNavigator.


Why don't you just clone and save current XPathNavigator?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #5
This is great - thanks!!

--
thanks - dave
"Chris Lovett" wrote:
There is no equivalent to getUniquePath() in the .NET XmlDocument object
model, but the docs on this method seem to indicate it does a very
simplistic calculation which is pretty easy to simulate as follows:

class NodeLocator {
private NodeLocator next;
private XmlNode node;

private NodeLocator(XmlNode node) {
this.node = node;
}

public static string GetUniqueLocation(XmlNode node) {
NodeLocator loc = new NodeLocator(node);
while (node.ParentNode != null ) {
node = node.ParentNode;
if (node is XmlDocument) break;
NodeLocator parentloc = new NodeLocator(node);
parentloc.next = loc;
loc = parentloc;
}
return loc.ToString();
}

public override string ToString() {
StringBuilder sb = new StringBuilder();
NodeLocator loc = this;
do {
sb.Append('/');
sb.Append(loc.node.Name);
sb.Append('[');
sb.Append(loc.IndexInParent);
sb.Append(']');
loc = loc.next;
} while (loc != null);
return sb.ToString();
}

private int IndexInParent {
get {
int indexInParent = 0;
XmlNode parent = node.ParentNode;
if (parent != null) {
foreach (XmlNode child in parent.ChildNodes) {
if (child == node) break;
if (child.Name == node.Name) {
indexInParent++;
}
}
}
return indexInParent;
}
}
}

This produces a path like "/x:foo[0]/x:item[1]". But this is a very
fragile XPath - any edits in the tree could break it since the hard coded
indexes [0] and [1] and so on, could now be wrong. A better XPath
expression would take into account the presence of unique ID's in the DOM
tree and use those as anchor points so that the resulting XPath location is
more robust in the face of subsequent DOM tree edits. So you would get
paths like: "/x:foo/x:item[@id='abc']". Now this XPath will work even if
you insert new items before this one, assuming the id's are still unique.
This could be done using the SchemaInfo provided on the XmlNodes and is left
as an exercise for the reader :-)

Chris.

"David Thielen" <th*****@nospam.nospam> wrote in message
news:B2**********************************@microsof t.com...
Hi;

I'm porting some java code over (that uses dom4j) and it includes the
ability to build up a new xpath using the getUniquePath() from another
select
as part of it. I can change the code to use the navigator objects but boy
will it be a PITA.

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:

> I may do a Select on a Select on a Select and have an XPathNavigator.
> But I
> want to later be able to get that same XPathNavigator by doing a single
> select on my original root XPathNavigator.

Why don't you just clone and save current XPathNavigator?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com


Nov 12 '05 #6
Chris Lovett wrote:
private int IndexInParent {
get {
int indexInParent = 0;


This should be int indexInParent = 1; as in XPath node position is 1-based.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #7
Good catch, thanks Oleg!

"Oleg Tkachenko [MVP]" <oleg@no_!spam!_please!tkachenko.com> wrote in
message news:eL**************@TK2MSFTNGP10.phx.gbl...
Chris Lovett wrote:
private int IndexInParent {
get {
int indexInParent = 0;


This should be int indexInParent = 1; as in XPath node position is
1-based.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #8

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

Similar topics

1
by: Craig Pearson | last post by:
Hi My function receives an XPathNavigator object. From here I need to build a DataSet to load into SQL Server (using SQLXML adaptor). Does anyone have an idea on the most efficient way to...
7
by: news.microsoft.com | last post by:
Hi, Is the compilation of XSLT, using XPathNavigator.Compile, only beneficial when the returned XPathExpression is cached? Next question, how do I cache the returned XPathExpression object? ...
1
by: Phil | last post by:
Is there an easy way to get the schema's data type for a XPathNavigator value? I need to be able to determine the data type for the xml value based off of an unknown schema and unknown xml file...
1
by: Bruce Dunwiddie | last post by:
I'm trying to build a couple classes that would allow for writing xsl transforms against data that is not originally xml. I've got an xmlreader implementation that seems to work well. Based on some...
12
by: David Thielen | last post by:
Hi; I have an element: <space> </space> When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not a String containing 1 space. Any ideas?
2
by: JSheble | last post by:
I'm integrating with a customer application, and in their assembly, when I call a method it returns data to us as an XPathNavigator object. I can parse the various elements I need out of this...
11
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString);...
4
by: Bruce Sandeman | last post by:
Hi, Does anyone know how to serialize an XPathNavigator object? I have tried the following but it moans that the xpn does not have a parameterless constructor. XPathNavigator xpn =...
2
by: =?Utf-8?B?Tm9yZW1hYw==?= | last post by:
Hi. Using VS2005, .NET 2.0. I have an xml document that I want to go through and set the values on attributes of elements. The elements are complex types defined in my schema (xsd) files. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.