views:

131

answers:

3

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.

A: 

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.

Jason Sperske