<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"  creationComplete="init()" height="100%" width="100%">

    <mx:Script>
        <![CDATA[ 
            private var songXML:XML =
                <songs>
                    <song title="My Dog has Fleas" artistID="100" />
                    <song title="I Hate Wet Socks" artistID="101" />
                    <song title="Hello to the Nice People" artistID="201" />
                    <song title="Moldy Baloney" artistID="202" />
                    <song title="Rainy Days" artistID="203" />
                    <song title="Cloudy Days" artistID="100" />
                    <song title="Mooney Nights" artistID="201" />
                    <song title="Sunburned Noggin" artistID="202" />
                </songs>;
                
            private var artistXML:XML =
                <artists>
                    <artist id="100" firstName="Sue"     lastName="Jones" />
                    <artist id="101" firstName="John"     lastName="Ambercrombie" />
                    <artist id="201" firstName="Ed"      lastName="Fish" />
                    <artist id="202" firstName="Jane"     lastName="Wolf" />
                    <artist id="203" firstName="Mary"     lastName="Birch" />
                </artists>;
            
            private function init():void
            {
                output_txt.text = "song xml:\n" + songXML.toXMLString() + "\n\n";
                output_txt.text += "artist xml:\n" + artistXML.toXMLString() + "\n\n";
                
                // get all songs written by artists with last names begining with 
                // A-G.
                
                function songsByArtistAlphaRange(songNode:XML, letterRange0:String, letterRange1:String):Boolean
                {
                    var artistID:String = songNode.@artistID;
                    var code0:Number = letterRange0.charCodeAt(0);
                    var code1:Number = letterRange1.charCodeAt(0);
                    
                    var artistNodes:XMLList = artistXML.artist.(@id==artistID && @lastName.charCodeAt(0) >= code0 && @lastName.charCodeAt(0) <= code1);
                    var bKeep:Boolean = artistNodes.length() > 0;
                    
                    if (bKeep){
                        var artistNode:XML = artistNodes[0];
                        // Actually changing the node we are filtering. Probably an evil thing to
                        // do.  Doing this for wackiness sake.
                        songNode.@artistName = artistNode.@firstName + " "  + artistNode.@lastName;    
                    }
                    
                    return bKeep;
                }
                
                // Make a copy so we don't corrupt the original -- the above callback function
                // intentionally corrupts the xml.  
                var songs2XML:XML = songXML.copy();
                
                output_txt.text += "E4X Query using custom function:\n";
                output_txt.text += "songs2XML.song.(songsByArtistAlphaRange(valueOf(),'A','G'))\n\n";
                
                // select those songs by artists with last names beginning with the letters A-G.
                var songs:XMLList = songs2XML.song.(songsByArtistAlphaRange(valueOf(),'A','G'));
                
                output_txt.text += "Result xml:\n" + songs.toXMLString() + "\n\n";
                
                output_txt.text += "Songs by Artists A-G:\n";
                                
                for (var i:int = 0 ; i < songs.length() ; i++){
                    var song:XML = songs[i];
                    output_txt.text += song.@artistName + ": " + song.@title + "\n";
                }
            }    
        ]]>
    </mx:Script>


    <mx:TextArea width="100%" height="100%" id="output_txt"/>
    
</mx:Application>