Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

Checklist: XML Performance – The Contradiction

I’ve been doing some serious work with the XPathNavigator (and running into performance “issues” which has slowed up finishing this), so I went back to re-read the Checklist: XML Performance page.  Here’s what they listed for XPath queries:

  1. Use XPathDocument to process XPath statements.
  2. Avoid the // operator by reducing the search scope.
  3. Compile both dynamic and static XPath expressions.

This looks like a very valid list, until you actually try to implement it.  Turns out they are mutually exclusive.  By implementing #1, it forces you to break #2, since XPathNavigator.Select() always evaluates from the root, not from the context of the current cursor location.  XPathNavigator.Select() also makes #3 useless for everything except for queries off the root node, since you normally build XPath statements from the context of the current cursor location (a la XSLT Templates), but Select always evaluates from the root.

Kzu (aka Daniel Cazzulino) pointed out this issue with XPath and XPathNavigator a while ago (prior to being an MVP), and Dare noted that evaluating from the root node context was by design, since the current node context may cause issues with certain ancestor type queries.  But kzu replied (and this is my point, too) that this is no different that what happens in XSLT templates.  I tried this out in the May VS.Net 2005 CTP, and it still exhibits this behavior. 

Can we get an overloaded version of XPathNavigator.Select() and Evaluate() that adds a UseRootContext Boolean flag, so we can turn off this behavior?  It can default to the current processing, but when we use the override, switch to the current cursor context.  I’ve been messing with the Rotor System.Xml code, trying to figure out the best solution, and so far this is the best I can come up with.  I’ve tried to figure out a way to override this behavior with OO techniques, but since most of what needs to be adjusted is internal classes, I can not change the processing without re-writing the XPathDocument from scratch.

Oh, kzu's work around for the context issue can be found here.  An excellent read, and definitely something to use until the framework is updated.

Published Wednesday, June 16, 2004 7:08 AM by donxml
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

oleg@tkachenko.com (Oleg Tkachenko) said:

Weird, but I seems to be unable to repro the issue. XPathNavigator.Select() does work starting from a node XPathNavigator represents. E.g.
<a>
<title name="MSFT">
<price>27</price>
</title>
<title name="IBM">
<price>25</price>
</title>
<title name="FOOB">
<price>5</price>
</title>
</a>

XPathDocument doc = new XPathDocument("foo.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile("price > 10");
XPathNodeIterator ni = nav.Select("/a/title");
while (ni.MoveNext())
{
Console.WriteLine(ni.Current.Evaluate(expr, ni));
}

Gives me
True
True
False

as expected.
June 17, 2004 1:55 AM

Daniel Cazzulino said:

June 19, 2004 7:04 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems