简体   繁体   中英

Accessing a node-set in a JavaScript XPath query

I have a real simple question that I can't seem to find an answer to.

I want to compress two XPath statements (that are getting attribute values). I learned about the | operator, hearing how it returns node sets.

var getdata = xmldoc.evaluate
(
    '/foo/bar[@world=\''+hello+'\']/child::*/attribute::name
    |/foo/bar[@world=\''hello+'\']/child::*/attribute::id', 
    xmldoc, null, XPathResult.ANY_TYPE, null
);

To anyone wondering, no I do not format my evaluation strings that way ... though, I sort of like it now that I typed it out. Anyways, this is how I tested it out.

alert(getItemData.iterateNext().childNodes[0].nodeValue);

That works! But it only returns the first one. While writing this, I just tried .length and made a break through ... it's only counting one item. Was I deceived about the concept of | ? How can I get a set and then go through them?

XML document, as requested.

<?xml version="1.0" encoding="ISO-8859-1"?>
<foo>
    <bar world="hello" id="1">
        <subbar name="item1" id="2">
        </subbar>
    </bar>
    <bar world="bye" id="3">
        <subbar name="item2" id="4">
        </subbar>
    </bar>
</foo>

Edit: I am currently using a function that grabs the element rather than the attribute, but I would really like to know the other way. Unless what I am doing is the best way.

If JQuery is an option, it might be worth your while to check out their XML traversal library. A quick search pulled up an article here . I wrote up a very rough example of what the logic may look like after you import the xml document, which is explained in the link.

var hello = "foo";
$('bar[world=' + hello + '] > subbar').each(function () {
    // You'd want to save these values somewhere else, obviously.
    $(this).getAttribute(name);
    $(this).getAttribute(id);
});

The key here is the XPathResult type you use.

I have implemented a working sample for the same. Please refer the code at http://jsbin.com/eneso3/5/edit

Basically you have to use Iterator as result type sot hat we can iterate through them to get the text. Refer Xpath reference mentioned on the working code sample page.

Well your usage of the "pipe" is correct ( http://www.tizag.com/xmlTutorial/xpathbar.php ) so the only code that I can see might be off is a missing + in the second xpath command, but that might be pseudo code, so I would only count this as a half answer. As for the best practice, in my opinion I would grab the subbar element then grab it's attributes out where you need them an optimization like the one you've suggested obfuscates what data is being referenced. Seems too much of a mico-optimization, but this is just an opinion. Maybe you have a long list of attributes and you really are saving parsing time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM