473,385 Members | 1,888 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,385 software developers and data experts.

XPathNavigator.Matches doesn't work as I expect

Hi,
I cannot guess, why some XPath expressions in my code returns false and some
even errors, while the others returne true as I expected. I'am workin with VS
2008 Express Edition.

Dim xEl As XElement = <root><child1><child2/></child1></root>
Dim nav As XPathNavigator = xEl.CreateNavigator

nav.MoveToRoot()

Console.WriteLine(nav.Matches("*")) 'returns FALSE
Console.WriteLine(nav.Matches("root")) 'returns FALSE
Console.WriteLine(nav.Matches("/root")) 'returns FALSE

Console.WriteLine("--------------------------")

nav.MoveToFirstChild()

Console.WriteLine(nav.Matches("*")) 'OK, returns TRUE
Console.WriteLine(nav.Matches("child1")) 'OK, returns TRUE
Console.WriteLine(nav.Matches("child1[parent::root]")) 'OK, returns TRUE
Console.WriteLine(nav.Matches("//root/child1")) 'returns FALSE

Console.WriteLine(nav.Matches("*[self::child1]")) 'OK, returns TRUE
'Console.WriteLine(nav.Matches("self::child1")) 'raise error
'Console.WriteLine(nav.Matches("*/self::child1")) 'raise error

Console.ReadLine()
Sep 5 '08 #1
4 3145

"karuzo" <ka****@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...
Hi,
I cannot guess, why some XPath expressions in my code returns false and
some
even errors, while the others returne true as I expected. I'am workin with
VS
2008 Express Edition.

Dim xEl As XElement = <root><child1><child2/></child1></root>
Dim nav As XPathNavigator = xEl.CreateNavigator

nav.MoveToRoot()

Console.WriteLine(nav.Matches("*")) 'returns FALSE
Console.WriteLine(nav.Matches("root")) 'returns FALSE
Console.WriteLine(nav.Matches("/root")) 'returns FALSE
Your problem is that you're trying to use an XElement (and not a
full-fledged XDocument) for XPath, which screws up the XPath Document Model.

Consider: in XPath, an XML document has a root (aka document) _node_ ("/"),
and a root _element_ ("/*"). When you do MoveToRoot on XPathNavigator, you
navigate to the document node, and you'll need to MoveToFirstChild to
position it on the root element. When you deal with an XmlDocument or
XDocument, the document itself is that nameless root document node. However,
when you deal with an XML fragment, there's no document, so the topmost
node - that is, your "root" - becomes the document node for XDM purposes;
except that it doesn't really qualify as such, because the document node is
not supposed to be an element - it has its own distinct type. This seems to
confuse XPath provider here, so "root" ends up matching neither an element
(probably because an element should always have a parent node), nor "/"
(probably because "/" is not supposed to be an element node).

If you rewrite your example and make xEl an XDocument, you'll see that it
works as expected (though you'll need to MoveToFirstChild immediately after
MoveToRoot).
Sep 5 '08 #2
"Pavel Minaev" <in****@gmail.comwrote in message
news:O$**************@TK2MSFTNGP02.phx.gbl...
>
"karuzo" <ka****@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...
>Hi,
I cannot guess, why some XPath expressions in my code returns false and
some
even errors, while the others returne true as I expected. I'am workin
with VS
2008 Express Edition.

Dim xEl As XElement = <root><child1><child2/></child1></root>
Dim nav As XPathNavigator = xEl.CreateNavigator

nav.MoveToRoot()

Console.WriteLine(nav.Matches("*")) 'returns FALSE
Console.WriteLine(nav.Matches("root")) 'returns FALSE
Console.WriteLine(nav.Matches("/root")) 'returns FALSE

Your problem is that you're trying to use an XElement (and not a
full-fledged XDocument) for XPath, which screws up the XPath Document
Model.

Consider: in XPath, an XML document has a root (aka document) _node_
("/"), and a root _element_ ("/*"). When you do MoveToRoot on
XPathNavigator, you navigate to the document node, and you'll need to
MoveToFirstChild to position it on the root element. When you deal with an
XmlDocument or XDocument, the document itself is that nameless root
document node. However, when you deal with an XML fragment, there's no
document, so the topmost node - that is, your "root" - becomes the
document node for XDM purposes; except that it doesn't really qualify as
such, because the document node is not supposed to be an element - it has
its own distinct type. This seems to confuse XPath provider here, so
"root" ends up matching neither an element (probably because an element
should always have a parent node), nor "/" (probably because "/" is not
supposed to be an element node).

If you rewrite your example and make xEl an XDocument, you'll see that it
works as expected (though you'll need to MoveToFirstChild immediately
after MoveToRoot).
If this is true its very unsatisfactory. XPath does not define that the
root node should be the Document. Indeed in many cases its important that
it isn't. E.g applying XSL to a child node in a document should have that
child node treated as the root as far as any XPath present in the XSL is
concerned.

--
Anthony Jones - MVP ASP/ASP.NET

Sep 5 '08 #3
Thank you, Pavel,
it solves my problem. I still don't understand the errors in the last two
(commented) line of code but I doesn't matter much.
Regards, Martin

"Pavel Minaev" wrote:
>
"karuzo" <ka****@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...
Hi,
I cannot guess, why some XPath expressions in my code returns false and
some
even errors, while the others returne true as I expected. I'am workin with
VS
2008 Express Edition.

Dim xEl As XElement = <root><child1><child2/></child1></root>
Dim nav As XPathNavigator = xEl.CreateNavigator

nav.MoveToRoot()

Console.WriteLine(nav.Matches("*")) 'returns FALSE
Console.WriteLine(nav.Matches("root")) 'returns FALSE
Console.WriteLine(nav.Matches("/root")) 'returns FALSE

Your problem is that you're trying to use an XElement (and not a
full-fledged XDocument) for XPath, which screws up the XPath Document Model.

Consider: in XPath, an XML document has a root (aka document) _node_ ("/"),
and a root _element_ ("/*"). When you do MoveToRoot on XPathNavigator, you
navigate to the document node, and you'll need to MoveToFirstChild to
position it on the root element. When you deal with an XmlDocument or
XDocument, the document itself is that nameless root document node. However,
when you deal with an XML fragment, there's no document, so the topmost
node - that is, your "root" - becomes the document node for XDM purposes;
except that it doesn't really qualify as such, because the document node is
not supposed to be an element - it has its own distinct type. This seems to
confuse XPath provider here, so "root" ends up matching neither an element
(probably because an element should always have a parent node), nor "/"
(probably because "/" is not supposed to be an element node).

If you rewrite your example and make xEl an XDocument, you'll see that it
works as expected (though you'll need to MoveToFirstChild immediately after
MoveToRoot).
Sep 5 '08 #4
"Anthony Jones" <An***********@yadayadayada.comwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
If this is true its very unsatisfactory. XPath does not define that the
root node should be the Document.
Well, XPath 2.0 does explicitly call that type of node a "document node"
(this of course doesn't mean that it must necessarily be a ***Document class
in .NET). XPath 1.0 calls it a "root node" without further clarification,
but it does specify that it is distinct from element nodes:

http://www.w3.org/TR/xpath#data-model

The tree contains nodes. There are seven types of node:

- root nodes
- element nodes
- text nodes
- attribute nodes
- namespace nodes
- processing instruction nodes
- comment nodes

...

Every node other than the root node has exactly one parent, which is
either an element node or the root node.

...

The root node is the root of the tree. A root node does not occur except
as the root of the tree. The element node for the document element is a
child of the root node. The root node also has as children processing
instruction and comment nodes for processing instructions and comments that
occur in the prolog and after the end of the document element.

...

The root node does not have an expanded-name.

Note that this final requirement clearly marks root node as distinct from
element node. An element node _must_ have a non-empty expanded name (because
you can't have a nameless element in XML).
Indeed in many cases its important that it isn't. E.g applying XSL to a
child node in a document should have that child node treated as the root
as far as any XPath present in the XSL is concerned.
The XSLT spec explicitly says that there is always a single root (i.e.,
non-element node) in the source (input) tree, even though it allows that to
have more than one-child (i.e., represent an XML fragment rather than
document), "when the source tree is created in some other way, for example
by using the DOM".
Sep 6 '08 #5

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

Similar topics

1
by: Steve Taylor | last post by:
I'm experiencing bad performance with certain kinds of match queries. Using a custom XPathNavigator that wraps the usual navigator I can see that many more node visits are performed than should be...
0
by: bughunter | last post by:
This is more an observation than a request for assistance. I understand that the descendent-or-self axis (//) can be an inefficient way of writing a query if a more specific query is possible....
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?
0
by: Lieven | last post by:
Hey, I'm developing a medical application. Medical data is presented in xml files. I have to queries these xml files to retrieve some data, for example the birthday of the patient. This is...
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...
1
by: SteZgr | last post by:
I have implemented in .NET 2.0 a custom XPathNavigator for Xpath filtering on an object tree. So far it works. While analyzing the performance, I have noticed a lot of MoveToNextAttribute calls. ...
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 =...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.