Skip to content

Instantly share code, notes, and snippets.

@os0x
Forked from cho45/dollarX.js
Created July 30, 2008 07:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save os0x/3242 to your computer and use it in GitHub Desktop.
Save os0x/3242 to your computer and use it in GitHub Desktop.
simple $X
// simple version of $X
// $X(exp);
// $X(exp, context);
// @source https:/raw.github.com/gist/3242
function $X (exp, context) {
context || (context = document);
var expr = (context.ownerDocument || context).createExpression(exp, function (prefix) {
return document.createNSResolver(context.documentElement || context).lookupNamespaceURI(prefix) ||
context.namespaceURI || document.documentElement.namespaceURI || "";
});
var result = expr.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
// not ensure the order.
var ret = [], i = null;
while (i = result.iterateNext()) ret.push(i);
return ret;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment