开发者

Using XPath namespaces in Firefox

开发者 https://www.devze.com 2023-03-30 16:54 出处:网络
I am attempting to parse an xml file in Javascript while ignoring the namespace of the xml file.I don\'t anticipate the namespace of the xml file changing in the future, though I\'d be happier if the

I am attempting to parse an xml file in Javascript while ignoring the namespace of the xml file. I don't anticipate the namespace of the xml file changing in the future, though I'd be happier if the code would work even if the namespace did change arbitrarily. I attempted to follow these instructions unsuccessfully.

What do I need to change so that my second version successfully finds the xml node? Below is my code, which does not work as expected. Speci开发者_JAVA技巧fically, the third call to Components.utils.reportError outputs null).

Javascript (in a firefox extension):

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);

XML File:

<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>

Output:

XML A: [object Element]
Test XPath: //OutputBlob:entry
null

The variation on my code found below (i.e., where namespaces are not involved) works as expected. That is, the third call to Components.utils.reportError does not output null.

Javascript (in a firefox extension):

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 File:

<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>

Output:

XML A: [object Element]
Test XPath: //OutputBlob
[object Element]


Fix: Replace

var testxpath = '//' + docky.documentElement.tagName + ":entry";

with

var testxpath = '//entry:' + docky.documentElement.tagName;
0

精彩评论

暂无评论...
验证码 换一张
取 消