Count attributes amount within one E4X expression

February 10th, 2010
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="1024" minHeight="768"
				creationComplete="application1_creationCompleteHandler(event)">
	<mx:Script>
		<![CDATA[
            import mx.events.FlexEvent;
            import mx.utils.ObjectUtil;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                var myXml:XML =
                    <deals name="bbs" type="C">
                        <deal name="sf" id="3" type="D"/>
                        <deal name="fdsf" id="1" type="E" description="testtest">
                            <deal name="basdf" id="2" type="A"/>
                        </deal>
                    </deals>;
                var names:Object = {};

                var attributes:XMLList = myXml..@*.(names[localName()] == null ?
                                                    names[localName()] = 1 :
                                                    names[localName()]++);
                trace(ObjectUtil.toString(names));
            }
        ]]>
    </mx:Script>
</mx:Application>

The output should be:

(Object)#0
  description = 1
  id = 3
  name = 4
  type = 4

Leave a Reply