Re: XPathNavigator.Matches doesn't work as I expect

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Pavel Minaev" <int19h@xxxxxxxxx> wrote in message news:O$s24o1DJHA.3904@xxxxxxxxxxxxxxxxxxxxxxx

"karuzo" <karuzo@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:24F9B3F5-154D-4D74-A7C3-4CBD42230FA5@xxxxxxxxxxxxxxxx
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

.



Relevant Pages

  • Returning "nearest in document" matches using XPath
    ... so there is only one "nearest" IP address corresponding to each ID. ... currently using the following XPath: ... only the node which matches nearest the root of the XML document. ... Querying from a sub-node: ...
    (comp.text.xml)
  • Re: XPathNavigator.Matches doesnt work as I expect
    ... full-fledged XDocument) for XPath, which screws up the XPath Document Model. ... the document itself is that nameless root document node. ... works as expected (though you'll need to MoveToFirstChild immediately after ...
    (microsoft.public.dotnet.xml)
  • RE: XPath generation question
    ... "/node1" in XPath means to search the node from the root. ... If the current node is the root, there is no different between these two ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.xml)
  • Re: Returning "nearest in document" matches using XPath
    ... Nick Leverton escribió: ... > currently using the following XPath: ... I dont see this reflected in the XML document. ... I want to find the left-most one in the above diagram, nearest to the root ...
    (comp.text.xml)
  • Re: XPathNavigator.Matches doesnt work as I expect
    ... full-fledged XDocument) for XPath, which screws up the XPath Document Model. ... the document itself is that nameless root document node. ... works as expected (though you'll need to MoveToFirstChild immediately after ...
    (microsoft.public.dotnet.xml)