javascript - XPath .evaluate() not returning anything - Stack Overflow

I'm having an issue getting XPath to return anything. I've only tried in Firefox, but I'

I'm having an issue getting XPath to return anything. I've only tried in Firefox, but I've used a bunch of different examples and none of them are working.

function populateFilters(productType) {
callAjax.request({
    url: './Products.xml',
    onSuccess: function(rootNode, fullText) {
        var path = '//product';
        // code for IE
        if (window.ActiveXObject)
            var nodes=rootNode.selectNodes(path);
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument)
            var nodes=rootNode.evaluate(path, rootNode, null, 0, null);

        for (var i = 0; i < nodes.length; ++i) {
            nodes[i].childNodes[0];
        }
    },
    onError: function(errCode, responseStatus) {alert('An error has occured. Please contact the site\'s Webmaster.\n\n' + errCode + '\n' + responseStatus);}
    });
}

The ajax call is working fine... I'm getting the XML document and am able to navigate the DOM without issue. The problem I'm having is when the code hits the rootNode.evaluate() call, nothing is returned. No errors, and no data. Here's an example snippet of the XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <product>
        <type>Snowboard</type>
        <brand>DC</brand>
    </product>
    <product>
        <type>Skateboard</type>
        <brand>Banana</brand>
    </product>
    <product>
        <type>Clothing</type>
        <brand>BoardDokter</brand>
    </product>
</root>

The only thing I can think of is that the var path = '//product'; is incorrect, but I've looked at many examples, and it really should work.

Anyone have any ideas? Firefox 4 on Windows XP, and on Windows 7.

I'm having an issue getting XPath to return anything. I've only tried in Firefox, but I've used a bunch of different examples and none of them are working.

function populateFilters(productType) {
callAjax.request({
    url: './Products.xml',
    onSuccess: function(rootNode, fullText) {
        var path = '//product';
        // code for IE
        if (window.ActiveXObject)
            var nodes=rootNode.selectNodes(path);
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument)
            var nodes=rootNode.evaluate(path, rootNode, null, 0, null);

        for (var i = 0; i < nodes.length; ++i) {
            nodes[i].childNodes[0];
        }
    },
    onError: function(errCode, responseStatus) {alert('An error has occured. Please contact the site\'s Webmaster.\n\n' + errCode + '\n' + responseStatus);}
    });
}

The ajax call is working fine... I'm getting the XML document and am able to navigate the DOM without issue. The problem I'm having is when the code hits the rootNode.evaluate() call, nothing is returned. No errors, and no data. Here's an example snippet of the XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <product>
        <type>Snowboard</type>
        <brand>DC</brand>
    </product>
    <product>
        <type>Skateboard</type>
        <brand>Banana</brand>
    </product>
    <product>
        <type>Clothing</type>
        <brand>BoardDokter</brand>
    </product>
</root>

The only thing I can think of is that the var path = '//product'; is incorrect, but I've looked at many examples, and it really should work.

Anyone have any ideas? Firefox 4 on Windows XP, and on Windows 7.

Share Improve this question edited Apr 5, 2013 at 14:00 Cristian Lupascu 40.6k18 gold badges105 silver badges138 bronze badges asked Apr 7, 2011 at 21:24 SivvySivvy 85210 silver badges28 bronze badges 2
  • 2 I'm suspicious of the "example snippet" of the XML source. Are you sure it doesn't have any namespaces in it? The most mon cause of this problem is that people don't realise namespaces matter, so they remove them from the source XML that they post. – Michael Kay Commented Apr 7, 2011 at 22:20
  • Nope. This system is just something I'm quickly making for a friend so he can populate his webpage until he can get a database. No namespaces. What you see in the snippet was actually copy/paste straight from the actual document. I removed a few products for the sake of length, but that's about it. – Sivvy Commented Apr 8, 2011 at 0:21
Add a ment  | 

1 Answer 1

Reset to default 3

document.evaluate returns an XPathResult object whose useful methods depend on the arguments:

  • unordered (4) and ordered (5) iterators give you an object with iterateNext(), which returns each node and then returns null.
  • unordered (6) and ordered (7) snapshots give you an object with snapshotLength and snapshotItem(index).
  • any (0) will give you an unordered iterator if your XPath expression returns nodes.

Since the fourth argument to evaluate is 0, it gave you an unordered iterator which you have to call iterateNext() on. This is probably not the one you want (most people want a guaranteed order).

See the Mozilla documentation on document.evaluate or the W3C reference on document.evaluate for more details.

var xml = '<?xml version="1.0" encoding="ISO-8859-1"?>\n' +
  '<root>\n' +
  '    <product>\n' +
  '        <type>Snowboard</type>\n' +
  '        <brand>DC</brand>\n' +
  '    </product>\n' +
  '    <product>\n' +
  '        <type>Skateboard</type>\n' +
  '        <brand>Banana</brand>\n' +
  '    </product>\n' +
  '    <product>\n' +
  '        <type>Clothing</type>\n' +
  '        <brand>BoardDokter</brand>\n' +
  '    </product>\n' +
  '</root>';
var path = '//product';
var doc = (new DOMParser).parseFromString(xml, 'text/xml');

// choice 1: use iterator
var it = doc.evaluate(path, doc, null, 5, null);
var nodes1 = [];
var node;
while (node = it.iterateNext()) {nodes1.push(node);}

// choice 2: use snapshot
var snapshot = doc.evaluate(path, doc, null, 7, null);
var nodes2 = [];
for (var i = 0; i < snapshot.snapshotLength; i++) {
  nodes2.push(snapshot.snapshotItem(i));
}

Handy tip: type $x into your Javascript console within Chrome to see the source of their function to turn XPath into a Javascript array.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745619394a4636430.html

相关推荐

  • javascript - XPath .evaluate() not returning anything - Stack Overflow

    I'm having an issue getting XPath to return anything. I've only tried in Firefox, but I'

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信