DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jon has posted 3 posts at DZone. View Full User Profile

XPath Selection

  • submit to reddit
        //test post

//Select elements based on an xpath expression.  Modified from the sample provided at greasemonkey's wiki.  Replaced array copy of elements with a closure to return snapshot item.

//p = xpath expression
//context = context the expression is evaluated on. e.g., document.
//returns snapshot of the xpath evaluation (i.e., elements, possibly attributes).

	function $XPathSelect(p, context) 
	  if (!context) context = document;
	  var xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	  return function(x) { return xpr.snapshotItem(x); };