By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

XPath generation question

P: n/a
Hi;

We have a TreeView that represents an xml file (or it's schema is a more
accurate statement). We want to have a double click on a node in the tree
generate the XPath to get to that node.

For the full tree it's easy. We walk up the parents getting the name of each
node and put a / between each so we end up with /node1/node2 or
node1/node2/@attr1

Questions on this case:
1) should we start with /node1 or self::/node1?
2) if they click on the root should we use "self::" or "/"?

We also have cases where we have a tree from a SelectNodes() and then want
to handle a select from that. It seems to me that this case is no different -
that in one case we have an XmlDocument and the other an XmlNodeList. But an
XmlDocument is an XmlNode so is there a difference that matters between an
XmlNode and an XmlNodeList?

And if so, what should be the root of the xpath we create in this case?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
May 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* David Thielen wrote in microsoft.public.dotnet.xml:
>We have a TreeView that represents an xml file (or it's schema is a more
accurate statement). We want to have a double click on a node in the tree
generate the XPath to get to that node.

For the full tree it's easy. We walk up the parents getting the name of each
node and put a / between each so we end up with /node1/node2 or
node1/node2/@attr1

Questions on this case:
1) should we start with /node1 or self::/node1?
2) if they click on the root should we use "self::" or "/"?
I don't think there is a good reason to use the self axis in either case
and note that for the first question, your latter option is not valid
syntax.
>We also have cases where we have a tree from a SelectNodes() and then want
to handle a select from that. It seems to me that this case is no different -
that in one case we have an XmlDocument and the other an XmlNodeList. But an
XmlDocument is an XmlNode so is there a difference that matters between an
XmlNode and an XmlNodeList?
Well, an XmlNode is a single node, whereas an XmlNodeList is one or more
nodes, that seems an important difference but then I am not sure what
you are really asking here.
>And if so, what should be the root of the xpath we create in this case?
It might be impossible to create one. Consider this case, you have two
nodes in the node list like <a><b c=''/></aand <a><b c='' d=''/></a>,
in order to create a path that would only match the c attribute in the
former example, you would have to write it so that it won't match if a
'd' attribute also occurs on the element. This might become arbitrarily
complicated. Of course if the nodes are always from a single document,
you would only look at single nodes and there would be no difference.
--
BjŲrn HŲhrmann ∑ mailto:bj****@hoehrmann.de ∑ http://bjoern.hoehrmann.de
Weinh. Str. 22 ∑ Telefon: +49(0)621/4309674 ∑ http://www.bjoernsworld.de
68309 Mannheim ∑ PGP Pub. KeyID: 0xA4357E78 ∑ http://www.websitedev.de/
May 14 '07 #2

P: n/a
Hi;

Sorry - self::node().

We've found that self::node() works and "/" does not in some cases passing
it to an XPathNavigator for SelectNodes().

For your example we do not handle that case - what we handle is if they
wanted the d='abc' attribute to equal a given value such as 'abc'.

The issue is how should we build up the xpath for that query as we could be
querying against an XmlDocument or an XPathNavigator that is at a location
within the document?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm


"Bjoern Hoehrmann" wrote:
* David Thielen wrote in microsoft.public.dotnet.xml:
We have a TreeView that represents an xml file (or it's schema is a more
accurate statement). We want to have a double click on a node in the tree
generate the XPath to get to that node.

For the full tree it's easy. We walk up the parents getting the name of each
node and put a / between each so we end up with /node1/node2 or
node1/node2/@attr1

Questions on this case:
1) should we start with /node1 or self::/node1?
2) if they click on the root should we use "self::" or "/"?

I don't think there is a good reason to use the self axis in either case
and note that for the first question, your latter option is not valid
syntax.
We also have cases where we have a tree from a SelectNodes() and then want
to handle a select from that. It seems to me that this case is no different -
that in one case we have an XmlDocument and the other an XmlNodeList. But an
XmlDocument is an XmlNode so is there a difference that matters between an
XmlNode and an XmlNodeList?

Well, an XmlNode is a single node, whereas an XmlNodeList is one or more
nodes, that seems an important difference but then I am not sure what
you are really asking here.
And if so, what should be the root of the xpath we create in this case?

It might be impossible to create one. Consider this case, you have two
nodes in the node list like <a><b c=''/></aand <a><b c='' d=''/></a>,
in order to create a path that would only match the c attribute in the
former example, you would have to write it so that it won't match if a
'd' attribute also occurs on the element. This might become arbitrarily
complicated. Of course if the nodes are always from a single document,
you would only look at single nodes and there would be no difference.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
May 14 '07 #3

P: n/a
Hi Dave,

"/node1" in XPath means to search the node (node1) from the root.
"self::node1" in XPath means to search the node (node1) from the current
node.
If the current node is the root, there is no different between these two
XPath.
However, if the current node is not the root of XML, the result executed by
these XPath will be different.

For example:
/
-Node1
-Node2
-Node3
-Node4
-Node5

If the current node is / (root), you can get the node(Node1) by both
"/Node1" and "self::Node1".
But if the current node is the node(Node4), the XPath"/Node1" will work,
but the "self::node1" will return null. Additionally, if you want to a get
the node(Node5), you should use "self::node5" or "/node3/node4/node5". But
"/node5" will return NULL.
"self::node5" is a relative location path and "/node3/node4/node5" is an
absolute location path.
>1) should we start with /node1 or self::/node1?
I would like to start with "/node1", because it is the absolute location
path.
>2) if they click on the root should we use "self::" or "/"?
I think "/" should be the root.

http://www.w3schools.com/xpath/xpath_axes.asp
[XPath Axes]

Hope this helps.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.