469,286 Members | 2,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

XPathNavigator - how to tell if a node exists?

Hi;

If I have an XPathNavigator object and for a given xpath statement need to
know if the node exists, how should I do this? I have found for some xpath
functions it returns an empty string if the node does not exist but that is
also the result for an empty node.

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

Cubicle Wars - http://www.windwardreports.com/film.htm
Jul 12 '07 #1
4 8531
* David Thielen wrote in microsoft.public.dotnet.xml:
>If I have an XPathNavigator object and for a given xpath statement need to
know if the node exists, how should I do this? I have found for some xpath
functions it returns an empty string if the node does not exist but that is
also the result for an empty node.
I do not know how you use the XPathNavigator, but using methods that
return strings is not the right approach; also note that if your XPath
expression evaluates to a string, there is no way to determine whether
some nodes relevant for the expression exist or not. A simple method is
to use nav.Evaluate("boolean(...)") and cast the result to a boolean.
--
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/
Jul 12 '07 #2
The problem with that is a user can feed us anything for the xpath -
including true(). What we really need is something that returns any of the
following:
1) not a legit xpath statement
2) xpath to a node - node exists
3) xpath to a node - node does not exist
4) other - like name(/root/node)

--
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:
If I have an XPathNavigator object and for a given xpath statement need to
know if the node exists, how should I do this? I have found for some xpath
functions it returns an empty string if the node does not exist but that is
also the result for an empty node.

I do not know how you use the XPathNavigator, but using methods that
return strings is not the right approach; also note that if your XPath
expression evaluates to a string, there is no way to determine whether
some nodes relevant for the expression exist or not. A simple method is
to use nav.Evaluate("boolean(...)") and cast the result to a boolean.
--
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/
Jul 12 '07 #3
* David Thielen wrote in microsoft.public.dotnet.xml:
>The problem with that is a user can feed us anything for the xpath -
including true(). What we really need is something that returns any of the
following:
1) not a legit xpath statement
2) xpath to a node - node exists
3) xpath to a node - node does not exist
4) other - like name(/root/node)
You can use the .Compile method which will return a XPathExpression
object which has a returnType property of type XPathResultType which
in turn will allow you to distinguish these cases. Evaluating the ex-
pression will then tell you whether there are matching nodes.
--
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/
Jul 12 '07 #4
great idea - thanks
--
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:
The problem with that is a user can feed us anything for the xpath -
including true(). What we really need is something that returns any of the
following:
1) not a legit xpath statement
2) xpath to a node - node exists
3) xpath to a node - node does not exist
4) other - like name(/root/node)

You can use the .Compile method which will return a XPathExpression
object which has a returnType property of type XPathResultType which
in turn will allow you to distinguish these cases. Evaluating the ex-
pression will then tell you whether there are matching nodes.
--
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/
Jul 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bruce Dunwiddie | last post: by
7 posts views Thread by David Thielen | last post: by
6 posts views Thread by David Thielen | last post: by
4 posts views Thread by Jack Fox | last post: by
4 posts views Thread by =?Utf-8?B?a2FydXpv?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.