Namespaces are used in XML to help group XML elements and attributes into sets of similar functionality; it is used especially in RSS.
In the XML document, Namespace is defined with an attribute xmlns to which is assigned (with colon : ) a prefix (a name) for Namespace name, and a string for its value, which is usually a URI (Uniform Resource Identifier) address.
The syntax is:

For example:
<pictures xmlns:ns="">
- "ns" represents the name of the Namespace, it can be any word.
- "" is the identification value for this Namespace (this can be any string, but usually it's a URI adresses, which can be non-valid URI).

The URI for an XML namespace doesn't have anything to do with a web site located there; it's only to provide a unique string.

Elements and attributes with the same Namespace name are considered as being part of the same group.
Namespaces can be declared in the element which uses it or in the root. You can define multiple namespaces in the same tag.

Working with namespace in AS3

There are multiple XML class methods in ActionScript used for working with namespace:
In E4X, XML namespace attributes are not represented as attributes (they cannot be accessed via attributes() or someElement.@*). They are accessed via namespace() method.
// Variable with XML data
var galery:XML = <pictures xmlns:ns="">

trace(galery.image[0].namespace('ns'));      //
trace(galery.image[1].namespace('ns'));      // Error #1010: A term is undefined

• To add a namespace dynamically to an XML object in ActionScript, you must create the namespace in an instance of the Namespace class.
The prefix (namespaceName) and the value of the namespace can be added as arguments when the instance is created:
new Namespace("prefix", "value_uri");
Then use this instance as argument in the "addNamespace()" method.

Or you can create the Namespace instance without arguments, and then add the "prefix" and its value by using the properties of the Namespace class: prefix and uri. These properties can also be used to access and edit later the name and the value stored in the Namespace object.
var name_var:Namespace = new Namespace();
name_var.prefix = "namespaceName";
name_var.uri = "value_uri";

Let's see an example. We define a simple XML format, with two tags in the root, and an attribute in the second tag.
Then we create a namespace and set it to the first element and to the attribute in the second tag.
// Variable with XML data
var test:XML = <root>
    <tag>Text elm1</tag>
    <tag atr="val">Elm 2</tag>

// define the instance with the namespace
var ns1:Namespace = new Namespace('ns', '');

// Add the namespace in the XML object (in root)

test.tag[0].setNamespace(ns1);         // Sets "ns" to the first <tag>

// Sets "ns" at the "atr" attribute in the second <tag>
// Or:        test.children()[1].@atr.setNamespace(ns1);

- This example displays in Output the following XML format:
<root xmlns:ns="">
  <ns:tag>Text elm1</ns:tag>
  <tag ns:atr="val">Elm 2</tag>
- As you can notice, the namespace was added in the XML content, in the root tag, and set to the first <tag> and to "atr".

• tag[index] accesses the tags without a namespace, so the "tag[0]" returns the first element without a namespace, not strictly the first element in the XML object. To avoid confusion that appears in these cases, to use the order in which the elements are in the XML object, you can use the "children()" method.
For example:         test.children()[1].@atr.setNamespace(ns1);

Using the double-colon operator (::)

E4X defines another operator for accessing nodes with namespaces: the double colon (::).
With this operator you can access nodes (elements, attributes) that have the same namespace.
First, use the namespace('prefix') method to add in a Namespace instance the nodes which have the "prefix" namespace, then you can access them with the double-colon (::) operator, using the following syntax:
To understand it better, see the following example:
// Variable with XML data
var galery:XML = <pictures xmlns:ns="">
    <ns:image ns:title="Img 2">img2.png</image>

// Add in a namespace instance all the elements (and attributes) with "ns" 
var name_s:Namespace = galery.namespace('ns');

trace(galery.name_s::image[0]);                     // img1.jpg
trace(galery.name_s::image[1].@name_s::title);      // Img 2
- Notice in the XML format how the namespace is applied to elements and attributes (ns:tag , ns:attribute).
The instance "name_s" stores the items which have the "ns" namespace (in a numeric order, like an Array), and then these items are accesed with the (::) operator, as presented in the syntax above.

The (::) operator can also be used to edit and delete the elements stored in the Namespace instance.
For example:
galery.name_s::image[1].@name_s::title = 'Another title';
- Modify the value of the "title" attribute in the second <image> element.

And the expression:
delete galery.name_s::image[0];
- Delete the first <image> element.

• Elements with namespace in a XML object can be accessed through hierarchical methods ( child((), children() ), or through a Namespace instance and the double-colon (::) operator.
The expression "element[index]" accesses only the elements without namespace, as seen in the following example.
// Variable with the XML data
var test:XML = <root xmlns:ns="">
    <ns:tag>Text elm1</ns:tag>
    <tag ns:atr="val">Elm 2</tag>

// Returns the value of the first element, using tag[0]
trace(test.tag[0]);          // Elm 2   (it is the second, but the first without namespace)

// Returns the value of the first element, using children()[0]
trace(test.children()[0]);         // Text elm1

To download the FLA file with the examples from this lesson, click: Working with XML Namespaces in AS3.

Daily Test with Code Example

Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
Indicate the PHP variable that contains data added in URL address after the "?" character.
if(isset($_GET["id"])) {
  echo $_GET["id"];
Working with XML Namespaces in ActionScript

Last accessed pages

  1. Multiple Select Dropdown List with AJAX (15558)
  2. Moving html element to a random direction (84)
  3. XHTML References - Old (15)
  4. Star shapes with CSS (4464)
  5. Get and Modify content of an Iframe (12618)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (387)
  2. Register and show online users and visitors (355)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (327)
  4. Insert, Select and Update NULL value in MySQL (256)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (254)