Forum Moderators: phranque

Message Too Old, No Replies

XML - efficient xpath

Absolute, indirect, or relative to a referenced node

         

broniusm

8:23 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



It is clear that an indirect xpath is slower than an absolute path:
//Person["Bob"]
vs
/Society/Beings/Person["Bob"]

But if I have a repetitive process on a particular subnode, does anyone know if it's indeed more efficient to go relative to that node versus specifying the whole path again:

at the Context node: /Society/Beings
Person
vs
/Society/Beings/Person

btw: Please correct my misuse of terminology.
thanks!

choster

3:24 pm on Jul 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tend to use relative references. The XSL is easier to read, fewer bytes need to be streamed, and most of our schemas are simple enough that subelements are never duplicated across node trees. As to performance, however, I could not say definitively which is the way to go.

The //Person["Bob"] selector is slower because of the //, not because of the Person["Bob"]-- the parser must scan the entire XML document for all instances of Person, then locate the Bobs. It will look for /Society/Person and /Society/Entities/Person and /Society/Language/Grammar/Person. On the other hand, given /Society/Beings/Person["Bob"] it will jump immediately to the specific case of Person being inside Beings, and then find Bob.

If you are already "inside" /Society/Beings, however, and recursively processing all Persons, there is no need to go back to /Society unless you are also doing processing on /Society/Entities/Persons and need to be explicit about which set you're using at the moment.

rpking

3:38 pm on Jul 1, 2003 (gmt 0)

10+ Year Member



Slightly off topic, but here's an excellent article [15seconds.com] about XML/XSLT efficiency.

Huge speed gains can be made by re-structuring your XML, before you even consider your XSLT...

drbrain

3:43 pm on Jul 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, efficiency of an XPath would be different depending upon XPath implementations. What may be faster on one implementation may or may not give you an equivalent gain on other implementations.

broniusm

7:46 pm on Jul 1, 2003 (gmt 0)

10+ Year Member



Thanks for all the responses. Still waiting to get to reading the "XML Database" article above-- thanks!

What may be faster on one implementation may or may not give you an equivalent gain on other implementations.

Ahh, that's why they call you the Dr. Good point-- this particular implementation is in jscript, clientside, something like this:


// original xpath:
var node = dom.selectNodes('/Society/Beings');

// then subsequent calls would go from node itself:
var sub = node[i].selectSingleNode('Person["+str+"]);

Anyhow, it was just a theoretical question that struck my curiosity-- in the given example, it is obvious which of the three xpath styles to use; I was just curious if someone had any hard evidence to support that the xml engine (MSXML2.DomDocument in my case) indeed maintains reference to a particular node thereby making it intrinsically faster to go relative to an already selected node, if if the pointer is in fact to the "xpath" again..

eh. I guess I got lonely in our 4-man development staff yesterday! :)