[Javascript] How not to use XPath

Hakan hakan at backbase.com
Tue Jul 5 07:55:20 CDT 2005


Hi Jan,

Personally, I love XPath. We have a fairly complete implementation in 
our RIA framework, and when you have it available you realize how 
incredibly useful it is. Especially when dealing with form data.

There's always performance issues to take into consideration, though. In 
your example, for example, the //-operation can be extremely expensive 
if you're dealing with a large dataset/huge application, since it's 
basically running over the entire DOM structure to get what you want.

This can be avoided by some clever naming and usage of unique ids, of 
course. Starting much deeper in the structure will naturally make it run 
over less nodes, thus executing faster.

I've found that a combination of XPath and "hard-coded" direct 
references (through id's or global javascript pointers) makes for a 
powerfull yet very generic solution.

Also, the syntax is really easy, once you "get it". It looks a bit 
confusing at first, I admit.

Regards,
H

Jursa, Jan (init) wrote:
> Hi,
> 
> so far i got along with not using XPath. 
> But nowadays with Ajax and Greasemonkey and all that cool stuff going
> on, I fear I will have to dive into that horrible syntax.
> 
> On the other hand, regarding this example...
> 
> 
> var images = document.getElementsByTagName("img"); 
> for (i = 0; i < images.length; i++) { 
> 	var n = imagesi; var src = n.getAttribute("src"); 
> 	if ((src) && (src.indexOf("threadmap_inactive.gif") > 0)) { 
> 	//do stuff here 
> 	} 
> }
> 
> 
> which could become this in XPath (supposedly):
> 
> 
> //img[contains(@src, 'threadmap_inactive.gif')]
> 
> (taken from:
> http://www.codestore.net/store.nsf/unid/BLOG-20050420?OpenDocument)
> 
> 
> ...i must admit it *is* much shorter...  :-(
> 
> 
> 
> 
> what are your experiences with XPath?
> 
> Cheers,
> Jan
> 
> 
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> 



More information about the Javascript mailing list