PackageTop Level
Classpublic final class Namespace
InheritanceNamespace Inheritance Object

The Namespace class contains methods and properties for defining and working with namespaces. There are three use cases for using namespaces:
  1. Namespaces of XML Objects
    Namespaces associate a namespace prefix with a Uniform Resource Identifier (URI) that identifies the namespace. The prefix is a string used to reference the namespace within an XML object. If the prefix is undefined, when the XML is converted to a string, a prefix is automatically generated.
  2. Namespace to Differentiate Methods
    Namespaces can differentiate methods with the same name to perform different tasks. If two methods have the same name but separate namespaces, they can perform different tasks.
  3. Namespaces for Access Control
    Namespaces can be used to control access to a group of properties and methods in a class. If you place the group of properties and methods into a private namespace, those properties and methods will be unreachable by any code that does not have access to that namespace. You can grant access to the group of properties and methods by passing the namespace to other classes, methods or functions.

This class (along with the XML, XMLList, and QName classes) implements powerful XML-handling standards defined in ECMAScript for XML (E4X) specification (ECMA-357 edition 2).

View the examples.

See also
XML, XMLList, QName, ECMAScript for XML (E4X) specification (ECMA-357 edition 2)


Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  prefix : String
The prefix of the namespace.
Namespace
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
  uri : String
The Uniform Resource Identifier (URI) of the namespace.
Namespace
Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods
 FunctionDefined by
  
Namespace(uriValue:*)
Creates a Namespace object given the uriValue parameter.
Namespace
  
Namespace(prefixValue:*, uriValue:*)
Creates a Namespace object, given the prefixValue and uriValue parameters.
Namespace
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
This function is equivalent to the Namespace.uri property.
Namespace
 Inherited
Returns the primitive value of the specified object.
Object
Property detail
prefix property
prefix:String  [read-write]

The prefix of the namespace.

Implementation
    public function get prefix():String
    public function set prefix(value:String):void
uri property
uri:String  [read-write]

The Uniform Resource Identifier (URI) of the namespace.

Implementation
    public function get uri():String
    public function set uri(value:String):void
Constructor detail
Namespace constructor

public function Namespace(uriValue:*)

Creates a Namespace object given the uriValue parameter. The values assigned to the uri and prefix properties of the new Namespace object depend upon the type of value passed for the uriValue parameter:

Parameters
uriValue:* — The Uniform Resource Identifier (URI) of the namespace.
Namespace constructor

public function Namespace(prefixValue:*, uriValue:*)

Creates a Namespace object, given the prefixValue and uriValue parameters. This constructor requires both parameters.

The value of the prefixValue parameter is assigned to the prefix property in the following manner:

The value of the uriValue parameter is assigned to the uri property in the following manner:

Parameters
prefixValue:* — The prefix to use for the namespace.
uriValue:* — The Uniform Resource Identifier (URI) of the namespace.
Method detail
toString method

public function toString():String

This function is equivalent to the Namespace.uri property.

Returns
String — The Uniform Resource Identifier (URI) of the namespace, as a string.
Class examples

The following example shows how to work with namespaces defined in XML objects. This is accomplished using the following steps:
  1. Define three Namespace properties, each with a unique URI that defines a namespace.
  2. Define an XML variable named myXML and assign it to the return value of getRSS(). The getRSS() method defines an XML object that contains several namespaces and returns that XML object.
  3. An Array variable is declared and populated by calling the parseRSS() method with myXML passed to it. In parseRSS(), the default XML namespace is defined as rss and an XMLList variable is defined by assigning the list of item objects in myXML. Then an Array is created and populated with various nodes within myXML.item and the Array is then returned.
  4. The elements in the Array are then printed using a for loop and three calls to trace().
package {
    import flash.display.Sprite;

    public class E4XNamespaceExample extends Sprite {
        private var rss:Namespace = new Namespace("http://purl.org/rss/1.0/");
        private var rdf:Namespace = new Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        private var dc:Namespace  = new Namespace("http://purl.org/dc/elements/1.1/");

        public function E4XNamespaceExample() {
            var myXML:XML = getRSS();
            var rssItems:Array = parseRSS(myXML);
            
            var len:uint = rssItems.length;
            for(var i:uint; i < len; i++) {
                trace(rssItems[i].title);
                trace(rssItems[i].creator);
                trace(rssItems[i].date);
                // Macromedia Flash Developer Center
                // Macromedia
                // 2005-08-08
                // Flex Developer Center
                // Macromedia
                // 2005-10-16                
            }
        }
        
        private function parseRSS(rssXML:XML):Array {
            default xml namespace = rss;

            var items:XMLList = rssXML.item;

            var arr:Array = new Array();            
            var len:uint = items.length();
            for(var i:uint; i < len; i++) {
                arr.push({title:items[i].title, creator:items[i].dc::creator, date:items[i].dc::date});
            }
            
            return arr;
        }

        private function getRSS():XML {
            var myXML:XML =  <rdf:RDF
              xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
              xmlns="http://purl.org/rss/1.0/"
              xmlns:dc="http://purl.org/dc/elements/1.1/"
            >
              <channel rdf:about="http://www.xml.com/cs/xml/query/q/19">
                <title>Test RSS</title>
                <link>http://www.macromedia.com/</link>
                <description>This is a test RSS document.</description>
                <language>en-us</language>
                <items>
                  <rdf:Seq>
                <rdf:li rdf:resource="http://www.macromedia.com/devnet/flash/"/>
                <rdf:li rdf:resource="http://www.macromedia.com/devnet/flex/"/>
                  </rdf:Seq>
                </items>
              </channel>
              <item rdf:about="http://www.macromedia.com/devnet/flash/">
                <title>Macromedia Flash Developer Center</title>
                <link>http://www.macromedia.com/devnet/flash/</link>
                <description>Welcome to the Flash Developer Center</description>
                <dc:creator>Macromedia</dc:creator>
                <dc:date>2005-08-08</dc:date>    
              </item>
              <item rdf:about="http://www.macromedia.com/devnet/flex/">
                <title>Flex Developer Center</title>
                <link>http://www.macromedia.com/devnet/flex/</link>
                <description>Welcome to the Flex Developer Center</description>
                <dc:creator>Macromedia</dc:creator>
                <dc:date>2005-10-16</dc:date>    
              </item>
            </rdf:RDF>;
            
            return myXML;
        }
    }
}

The following example shows how namespaces can be used to differentiate methods with the same name to perform different tasks. In this example, three methods named hello() reside in separate namespaces, and, as such, each returns a different string when called.
package {
    import flash.display.Sprite;

    public class NamespaceExample extends Sprite {    
        public function NamespaceExample() {
            var vocab:MultilingualVocabulary = new MultilingualVocabulary();

            trace(vocab.hello());    // hello
            
            var languages:Array = vocab.getLanguages();
            
            for(var i:uint; i < languages.length; i++) {
                var ns:Namespace = languages[i];
                if(ns != null) {
                    trace(ns.uri + ": " + vocab.ns::hello());
                    // MultilingualVocabularyMultilingualVocabulary:Hawaiian: aloha
                    // MultilingualVocabularyMultilingualVocabulary:French: bon jour
                }
            }
        }
    }    
}

class MultilingualVocabulary {
    private namespace French;
    private namespace Hawaiian;
    private var languages:Array;

    public function MultilingualVocabulary() {
        languages = new Array(Hawaiian, French);
    }
        
    public function hello():String { 
        return "hello";
    }

    Hawaiian function hello():String {
        return "aloha";
    }

    French function hello():String { 
        return "bon jour";
    }
        
    public function getLanguages():Array {
        return languages;
    }
}