Skip to content

Instantly share code, notes, and snippets.

@mitsugu
Last active June 9, 2024 23:36
Show Gist options
  • Save mitsugu/b80a80a77ed2b048760ef6f9845cdec1 to your computer and use it in GitHub Desktop.
Save mitsugu/b80a80a77ed2b048760ef6f9845cdec1 to your computer and use it in GitHub Desktop.
function evaluateXPath(prefix, aNode, aExpr) {
//
// prefix : namespace
// aNode : DOM node
// aExpr : XPath text
//
// return value : DOM element or DOM node
//
// See URL for xpath expressions
// https://developer.mozilla.org/ja/docs/Web/XPath/Introduction_to_using_XPath_in_JavaScript#implementing_a_user_defined_namespace_resolver
//
var resolver = function(prefix) {
var ns = {
'html' : 'http://www.w3.org/1999/xhtml',
'regular' : 'http://www.w3.org/2005/Atom',
'overallhead' : 'http://xml.kishou.go.jp/jmaxml1/informationBasis1/',
'overall' : 'http://xml.kishou.go.jp/jmaxml1/body/meteorology1/',
'rtd' : 'http://xml.kishou.go.jp/jmaxml1/',
'bdd' : 'http://xml.kishou.go.jp/jmaxml1/body/meteorology1/',
'jmx_eb' : 'http://xml.kishou.go.jp/jmaxml1/elementBasis1/',
'rtw' : 'http://xml.kishou.go.jp/jmaxml1/',
'bdw' : 'http://xml.kishou.go.jp/jmaxml1/body/meteorology1/'
};
return ns[prefix] || null;
};
var result = aNode.evaluate(
aExpr,
aNode,
resolver,
XPathResult.ANY_TYPE,
null
);
var found = [];
var res;
while (res = result.iterateNext()){
found.push(res);
}
return found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment