[XML] Counting XML items with same attributes

[SIZE=1]Hello. This code, which phorte gave me in a previous post, loops through an arbitrary array of lowercase letters and outputs each letter and how many times it appears in the array:[/SIZE]
ActionScript Code:
[FONT=Courier New][LEFT]var items:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#FF0000]“a”[/COLOR], [COLOR=#FF0000]“a”[/COLOR], [COLOR=#FF0000]“b”[/COLOR], [COLOR=#FF0000]“c”[/COLOR], [COLOR=#FF0000]“a”[/COLOR], [COLOR=#FF0000]“b”[/COLOR], [COLOR=#FF0000]“d”[/COLOR], [COLOR=#FF0000]“a”[/COLOR], [COLOR=#FF0000]“c”[/COLOR], [COLOR=#FF0000]“e”[/COLOR][COLOR=#000000]][/COLOR];
[COLOR=#000000]var[/COLOR] itemCount:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR];
[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR]i = [COLOR=#000080]0[/COLOR]; i < items.[COLOR=#0000FF]length[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR]j = [COLOR=#000080]0[/COLOR]; j <= itemCount.[COLOR=#0000FF]length[/COLOR]; j++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR]items[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] == itemCount[COLOR=#000000][[/COLOR]j[COLOR=#000000]][/COLOR].[COLOR=#000080]item[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]var[/COLOR] foundAt = j;
[COLOR=#0000FF]break[/COLOR];
[COLOR=#000000]}[/COLOR] [COLOR=#0000FF]else[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]var[/COLOR] foundAt = -[COLOR=#000080]1[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
if [COLOR=#000000]([/COLOR]foundAt == -[COLOR=#000080]1[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]var[/COLOR] n = itemCount.[COLOR=#0000FF]length[/COLOR];
itemCount[COLOR=#000000][[/COLOR]n[COLOR=#000000]][/COLOR] = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]Object[/COLOR]COLOR=#000000[/COLOR];
itemCount[COLOR=#000000][[/COLOR]n[COLOR=#000000]][/COLOR].[COLOR=#000080]item[/COLOR] = items[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR];
itemCount[COLOR=#000000][[/COLOR]n[COLOR=#000000]][/COLOR].[COLOR=#000080]count[/COLOR] = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]Object[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR] [COLOR=#0000FF]else[/COLOR] [COLOR=#000000]{[/COLOR]
itemCount[COLOR=#000000][[/COLOR]foundAt[COLOR=#000000]][/COLOR].[COLOR=#000080]count[/COLOR]++;
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR]i = [COLOR=#000080]0[/COLOR]; i < itemCount.[COLOR=#0000FF]length[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR]itemCount[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR].[COLOR=#000080]item[/COLOR] + [COLOR=#FF0000]" ("[/COLOR] + itemCount[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR].[COLOR=#000080]count[/COLOR] + [COLOR=#FF0000]")"[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]}[/COLOR]
[/LEFT]
[/FONT]

[SIZE=1]I am trying to apply this code to XML. I have an XML file of a list of artwork and the artists names. I am trying to generate a menu of artists names and how many pieces they have in the collection (i.e., how many times their name shows up.) Working off senocular’s “Squirrel Finder” tutorial, shouldn’t I be able to use the code above to count items in an array that I have defined in an XML variable?[/SIZE]

[SIZE=1]XML structure is similar to this:
[/SIZE]
[SIZE=1][COLOR=seagreen]<inventory>[/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen]<item type=“Ceramic” artistname1=“Jane” artistname2=“Doe” />[/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen]<item type=“Glass” artistname1=“Jane” artistname2=“Doe” />[/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen]<item type=“Sculpture” artistname1=“Fred” artistname2=“Smith” />[/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen]</inventory>[/COLOR][/SIZE]

[SIZE=1]when I implement the first code example into my ActionScript, like this:
[/SIZE]
[SIZE=1]
[/SIZE]

 
[COLOR=magenta]// create new XML object instance, remembering to ignore white space[/COLOR]
var inventory_xml = new XML();
inventory_xml.ignoreWhite = true;
[COLOR=magenta]// define an onLoad to output artists' names when the XML has successfully loaded.[/COLOR]
inventory_xml.onLoad = function(success){
if (success) {
[COLOR=magenta]// define elements to cycle through[/COLOR]
var inv = inventory_xml.firstChild;
var item = inv.childNodes;  [COLOR=magenta]//this defines array of menu items(?)[/COLOR]
var itemCount:Array = [];
[COLOR=magenta]// cycle through elements[/COLOR]
[COLOR=blue]for (i = 0; i < item.length; i++) {[/COLOR]
[COLOR=blue]for (j = 0; j <= itemCount.length; j++) {[/COLOR]
[COLOR=blue]if (item* == itemCount[j].item) {[/COLOR]
[COLOR=blue]var foundAt = j;[/COLOR]
[COLOR=blue]break;[/COLOR]
[COLOR=blue]} else {[/COLOR]
[COLOR=blue]var foundAt = -1;[/COLOR]
[COLOR=blue]}[/COLOR]
[COLOR=blue]}[/COLOR]
[COLOR=blue]if (foundAt == -1) {[/COLOR]
[COLOR=blue]var n = itemCount.length;[/COLOR]
[COLOR=blue]itemCount[n] = new Object();[/COLOR]
[COLOR=blue]itemCount[n].item = item*;[/COLOR]
[COLOR=blue]itemCount[n].count = new Object(1);[/COLOR]
[COLOR=blue]} else {[/COLOR]
[COLOR=blue]itemCount[foundAt].count++;[/COLOR]
[COLOR=blue]}[/COLOR]
[COLOR=blue]}[/COLOR]
for (i = 0; i < itemCount.length; i++) {
trace(itemCount*.item.attributes.artistname2 + " (" + itemCount*.count + ")");
}
}
}
[COLOR=magenta]// load the xml file![/COLOR]
inventory_xml.load("inventorylist.xml");

[SIZE=1]I get the following output:

[COLOR=blue]Doe (1)[/COLOR][/SIZE]
[SIZE=1][COLOR=blue]Doe (1)[/COLOR][/SIZE]
[SIZE=1][COLOR=blue]Smith (1)[/COLOR][/SIZE]

[SIZE=1]rather than

[COLOR=blue]Doe (2)[/COLOR][/SIZE]
[SIZE=1][COLOR=blue]Smith (1)[/COLOR][/SIZE]

[SIZE=1]I am trying to get these two codes to work together. Please help.

Thanks.[/SIZE]

[SIZE=1]PS. how do you wrap AS code tags (not <CODE>) around your code in these threads?
[/SIZE]