Re: XPathNavigator.Matches doesn't work as I expect
- From: "Anthony Jones" <AnthonyWJones@xxxxxxxxxxxxxxxx>
- Date: Fri, 5 Sep 2008 14:38:47 +0100
"Pavel Minaev" <int19h@xxxxxxxxx> wrote in message news:O$s24o1DJHA.3904@xxxxxxxxxxxxxxxxxxxxxxx
"karuzo" <karuzo@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:24F9B3F5-154D-4D74-A7C3-4CBD42230FA5@xxxxxxxxxxxxxxxxHi,
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
.
- Follow-Ups:
- Re: XPathNavigator.Matches doesn't work as I expect
- From: Pavel Minaev
- Re: XPathNavigator.Matches doesn't work as I expect
- References:
- XPathNavigator.Matches doesn't work as I expect
- From: karuzo
- Re: XPathNavigator.Matches doesn't work as I expect
- From: Pavel Minaev
- XPathNavigator.Matches doesn't work as I expect
- Prev by Date: Re: XPathNavigator.Matches doesn't work as I expect
- Next by Date: Re: XPathNavigator.Matches doesn't work as I expect
- Previous by thread: Re: XPathNavigator.Matches doesn't work as I expect
- Next by thread: Re: XPathNavigator.Matches doesn't work as I expect
- Index(es):
Relevant Pages
|