This gets the variables at the top level and gives me the correct count for each level.
function parseXml(xml) { $(xml).find('sites').each(function(){ var PARENT = $(this).children($(this).attr('id')); var PARENTcount = PARENT.length; var PARENTSchildren = $(PARENT).children($(this).attr('label')); var CHILDRENcount = PARENTSchildren.length; var CHILDSchildren = $(PARENTSchildren).children($(this).attr ('label')); var CHILDScount = CHILDSchildren.length; alert(PARENTcount) alert(CHILDRENcount) alert(CHILDScount) }); }