Consider this XML snippet:
<book id="5" />
<book id="15" />
<book id="5" />
<book id="25" />
<book id="35" />
<book id="5" />
How can I compose an E4X statement/query to get a list of the unique values in the id
attribute? I'm using E4X in a JavaScript non-browser context.
5
15
25
35
Is there a distinct()
or groupby()
method of any kind that would help? If not, how could this be accomplished?
There is no unique or groupby method. However, E4X allow you to quickly create an XMLList of the values non-unique values.
var xmlSnippet = <stuff><book id="5"/>
<book id="15"/>
<book id="5"/>
<book id="25"/>
<book id="35"/>
<book id="5"/>
</stuff>;
var attributes = xmlSnippet['book'].attribute("id");
To make it unique, you can then take the XMLList, iterate through it, and store each element as the key value in an object.
var uniqueAttributes = new Object ();
for each (var a in attributes)
{
uniqueAttributes[a] = null;
}
To access these values, iterate through them using a for in loop (not a for each in)
for (var u in uniqueAttributes)
{
// do something with u
}