javascript - Trouble with XPCOM XPathEvaluator -
i'm having trouble evaluating xpaths using nsidomxpathevaluator mozillas xpcom. i'm running following javascript code through xpcshell:
[...] var mypaths = new array(); mypaths[0] = "/"; mypaths[1] = "/node()"; mypaths[2] = "/html"; for(i in mypaths) { mypath = mypaths[i]; var document = doms[0]; var xpeval = components.classes["@mozilla.org/dom/xpath-evaluator;1"].createinstance(components.interfaces.nsidomxpathevaluator); var ns = xpeval.creatensresolver(document.documentelement); var type = components.interfaces.nsidomxpathresult.unordered_node_snapshot_type; var res = xpeval.evaluate(mypath, document.documentelement, ns, type, null); dump("\npath: "+mypath+"\n"); dump("result length: "+res.snapshotlength+"\n"); ( var i=0 ; < res.snapshotlength; i++ ) dump("... node: "+res.snapshotitem(i)+"\n"); dump("... ... .nodename: "+res.snapshotitem(i).nodename+"\n"); } [...]
the doms-list array of html-documents parsed using do_parse_document xpcshell testing utilities. top of documents are:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
my trouble xpaths use input doesn't quite return expect. below output above snippet:
path: / result length: 1 ... node: [object xmldocument] ... ... .nodename: #document path: /node() result length: 2 ... node: [object documenttype] ... ... .nodename: html ... node: [object htmlhtmlelement] ... ... .nodename: html path: /html result length: 0 path: //html result length: 0
i expect @ least 1 or 2 results /html , //html paths. (those returned when using /node() path)
i can't correct output when doing queries count(//p) either (that 1 returns 0 although there plenty of paragraphs in document).
i have tried calling document.evaluate() instead of xpeval.evaluate() same results. have tried passing null namespace, same results.
am making stupid mistake(s), or might there quirks in nsidomxpathevaluator means can't use how intend?
thanks time!
regards, torin
dump namespaceuri property of document.documentelement node, in xhtml namespace http://www.w3.org/1999/xhtml , in case xpath 1.0 select elements in namespace need bind prefix namespace uri , use prefix. api use have make sure namespace resolver resolves choosen prefix xhtml namespace uri. need e.g.
var nsresolver = function (prefix) { if (prefix === 'xhtml') return 'http://www.w3.org/1999/xhtml'; else return null; }; var res = xpeval.evaluate(mypath, document, nsresolver, type, null);
and paths /xhtml:html
or /xhtml:html/xhtml:body/xhtml:h1
.
Comments
Post a Comment