first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

E4X and ActionScript: “Using XML Namespaces with E4X and ActionScript 3” by Darron Schall

import mx.rpc.events.ResultEvent;

[Bindable]
public var rockArtists:XMLList;

// Define the namespace used in the rss feed for "itms".  Note that the
// namespace we define is "items" instead.  These values do not need
// to be the same, but generally should be for readability purposes.
namespace items = "http://phobos.apple.com/rss/1.0/modules/itms/";

private function onResult( event:ResultEvent ):void {  
    var rss:XML = event.result as XML;  

    // Filter the feed by the Rock category  
    var rock:XMLList = rss.channel.item.(category == "Rock");  

    // Use "items" namespace prefix (corresponding to the namespace we  
    // defined above) to access the artist elements  
    //rockArtists = rock.items::artist;  
    // Or, alternatively:  
    use namespace items;  
    rockArtists = rock.artist;  
}

The key here is that the artist element nodes are created with <itms:artist> in the RSS feed. In the XML, the "itms" namespace is defined as x­mlns:itms="http://phobos.apple.com/rss/1.0/modules/itms/". This namespace is re-created in ActionScript using the namespace keyword in ActionScript, but instead of calling it "itms" it is defined as "items". In general, you'd want to keep the prefix the same for both, but for illustration purposes you can see that they're allowed to be different.

Accessing the artist element nodes then is simply a matter of using the namespace via use namespace items and asking for just the artist (without the namespace prefix). Another approach is to omit the use namespace ... code and use the namespace prefix directly: rock.items::artist. In both cases, the namespace to use is the one defined in ActionScript, and not the one defined in the XML file (so "items" instead of "itms").

[http://www.darronschall.com/weblog/2006/04/ using-xml-namespaces-with-e4x-and-actionscript-3.cfm]

mod date: 2009-07-28T05:08:55.000Z