Javascript 在Firefox中使用XPath名称空间
我试图在Javascript中解析xml文件,同时忽略xml文件的名称空间。我不认为xml文件的名称空间将来会发生变化,不过如果代码能够正常工作,即使名称空间发生了任意变化,我会更高兴。我试图跟随,但没有成功 为了使第二个版本成功地找到xml节点,我需要更改什么?下面是我的代码,它不能按预期工作。具体来说,对Components.utils.reportError的第三次调用输出null firefox扩展中的Javascript:Javascript 在Firefox中使用XPath名称空间,javascript,xml,firefox,xpath,Javascript,Xml,Firefox,Xpath,我试图在Javascript中解析xml文件,同时忽略xml文件的名称空间。我不认为xml文件的名称空间将来会发生变化,不过如果代码能够正常工作,即使名称空间发生了任意变化,我会更高兴。我试图跟随,但没有成功 为了使第二个版本成功地找到xml节点,我需要更改什么?下面是我的代码,它不能按预期工作。具体来说,对Components.utils.reportError的第三次调用输出null firefox扩展中的Javascript: Components.utils.reportError("X
Components.utils.reportError("XML A: " + docky.documentElement);
var testxpath = '//' + docky.documentElement.tagName + ":entry";
Components.utils.reportError("Test XPath: " + testxpath);
Components.utils.reportError(docky.evaluate(testxpath, docky,
function(){return 'http://tempuri.org/';},
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue);
Components.utils.reportError("XML A: " + docky.documentElement);
var testxpath = '//' + docky.documentElement.tagName;
Components.utils.reportError("Test XPath: " + testxpath);
Components.utils.reportError(docky.evaluate(testxpath, docky, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue);
XML文件:
<OutputBlob xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.org/">
<rnd>
<double>0.23500252665719135</double>
<double>0.82559380951597994</double>
<double>0.33431208335529644</double>
<double>0.91389494431852125</double>
</rnd>
</OutputBlob>
<OutputBlob xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<rnd>
<double>0.23500252665719135</double>
<double>0.82559380951597994</double>
<double>0.33431208335529644</double>
<double>0.91389494431852125</double>
</rnd>
</OutputBlob>
下面我的代码的变体,即不涉及名称空间的地方,工作正常。也就是说,对Components.utils.reportError的第三次调用不会输出null
firefox扩展中的Javascript:
Components.utils.reportError("XML A: " + docky.documentElement);
var testxpath = '//' + docky.documentElement.tagName + ":entry";
Components.utils.reportError("Test XPath: " + testxpath);
Components.utils.reportError(docky.evaluate(testxpath, docky,
function(){return 'http://tempuri.org/';},
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue);
Components.utils.reportError("XML A: " + docky.documentElement);
var testxpath = '//' + docky.documentElement.tagName;
Components.utils.reportError("Test XPath: " + testxpath);
Components.utils.reportError(docky.evaluate(testxpath, docky, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue);
XML文件:
<OutputBlob xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.org/">
<rnd>
<double>0.23500252665719135</double>
<double>0.82559380951597994</double>
<double>0.33431208335529644</double>
<double>0.91389494431852125</double>
</rnd>
</OutputBlob>
<OutputBlob xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<rnd>
<double>0.23500252665719135</double>
<double>0.82559380951597994</double>
<double>0.33431208335529644</double>
<double>0.91389494431852125</double>
</rnd>
</OutputBlob>
修复:替换
var testxpath = '//' + docky.documentElement.tagName + ":entry";
与