Multimobile Development: Building Applications for any Smartphone
XPathNavigator.Evaluate bug?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.xml.


Daniel Cazzulino
GOOD ANSWER
This is the context:
If I have a simple xpath expression like: "price > 10"
And if I have selected nodes from an XPathDocument with another expression:
"//publishers/titles"

I want to execute the first expression in the context of the nodes selected
by the second one. I would use:

XPathNavigator nav = document.CreateNavigator();
XPathExpression expr = nav.Compile("price > 10");
XPathNodeIterator nodes = nav.Select("//publishers/titles");
//Finally execute the expression
object result = nav.Evaluate(expr, nodes); //ALWAYS RETURNS FALSE!!!

The result of the evaluation is always false because the expression is not
evaluated in the context of the nodes passed but starting from the root
document. The only way to make the expression work is replace the "price >
10" expression with "//publishers/titles/price > 10"
So I think I'm misundestanding the concept of the context. Maybe it's only
the filtered set of nodes against which to execute the same expression, but
I think it may be far more useful if the context applied to the expression
itself too, so we don't have to type the full XPath expression when we
already have a node. The expression should be evaluated in the "context" of
the nodes passed, just like XSLT context works.

Was this done on purpose?

Thanks,
Daniel

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Dare Obasanjo
GOOD ANSWER
This is an issue I've discussed in the past with the devs who own both XPath
& XSLT and have come to the conclusion that although irritating this
behavior is probably the best. The problem with using the current node as
the context is that one has to ask what queries like //titles/ancestor::*
may return. Does it return all the ancestors of the <titles> elements up to
the current node context or does it return all the ancestors in the
document?

We decided in our implementation to err on the side of allowing such queries
to operate on the whole document than on a subtree.

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Daniel Cazzulino" <Click here to reveal e-mail address> wrote in message
news:#y5TKurICHA.2480@tkmsftngp11...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Daniel Cazzulino
GOOD ANSWER
Well, I don't necessarily see a problem here... I think that just working
the way XSLT resolves XPath expressions would be just fine.
The issue is not about the nodes being returned, as the owner document for a
node is always known beforehand, and can't be changed nor removed. So an
expression asking for ancestor, siblings etc. MUST be evaluated in the
context of the whole document.
That's a different thing than saying what is the context for _resolving_ the
XPath expression, which SHOULD be the context nodes being passed in to the
Evaluate function, just like XSLT does. When an XSLT template matches some
node-set, the XPath expressions are resolved from there on...
So if the XSLT match is "//titles" and I happen to select a value-of
select="../*" there's no doubt I will be selecting the parent element and
ALL their children, whether they include the nodes matched or not. The same
way, if I choose value-of select="title_id" there's no doubt I'm selecting
the title_id child of the titles node (inside a for-each for example).
So IMO the proper way to handle the Evaluate method when I'm passing an
XPathNodeIterator list is by starting the XPath parsing from there. For
example:

XPathNavigator nav = document.CreateNavigator();
XPathExpression expr = nav.Compile("title_id");
XPathNodeIterator nodes = nav.Select("//publishers/titles");
object result = nav.Evaluate(expr, nodes);

The result variable should contain a node-set with all the children <price>
nodes of the node list passed in to the function. The question is not about
the context to execute the expression itself, but how the expression is
resolved in that context, which is always the same, obviously, either on
XSLT or on the DOM.

Daniel

"Dare Obasanjo" <Click here to reveal e-mail address> wrote in message
news:OgT3nPvICHA.2548@tkmsftngp11...
> This is an issue I've discussed in the past with the devs who own both
XPath
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
 
System.Xml.XPath.XPathDocument
System.Xml.XPath.XPathExpression
System.Xml.XPath.XPathNavigator
System.Xml.XPath.XPathNodeIterator




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734