Namespaces in Flex and Soprano

In XML, namespaces are used to avoid potential naming conflicts with other components having the same names. Since Flex lets you program in MXML and in ActionScript 3, I “ll go over the syntax of namespaces in both of them. Familiarity with Soprano family is a pre-requisite for reading this article.

Namespaces in MXML

MXML applications start with the lt;mx:Application gt; tag that includes xmlns property:

lt;mx:Application xmlns:mx= rdquo; rdquo; layout= “absolute ” gt;

The namespace mx:xmlns refers to the URI that lists valid MXML tags. Open the file flex-config.xml, and you “ll find there an XML element that links this URI to the file mxml-manifest.mxl, which list all MXML components. Here “s an extract from this manifest file:

lt;component id= “ButtonBar ” class= “mx.controls.ButtonBar “/ gt;

lt;component id= “Canvas ” class= “mx.containers.Canvas “/ gt;

lt;component id= “CheckBox ” class= “mx.controls.CheckBox “/ gt;

lt;component id= “ColorPicker ” class= “mx.controls.ColorPicker “/ gt;

lt;component id= “ComboBox ” class= “mx.controls.ComboBox “/ gt;

If you want to use one of the standard MXML components, specify the prefix mx for each of them. For example, to use MXML Label, you write the following:

lt;mx:Label x= “111 ” y= “81 ” text= “Hello World “/ gt;

If you are going to create custom Flex components, keep them in a separate namespace to avoid naming conflicts. For example, if you are going to develop a custom Tree component, introduce another namespace, for example with the URI com.enron.controls.* as shown below:

lt;mx:Application xmlns:mx= “

xmlns:lib= “com.enron.controls.* ” gt;


lt;lib:Tree id= “tree ” width= “50% ” height= “100% ” hellip; gt;

lt;/lib:Tree gt;

lt;/mx:Application gt;

This sample defines two namespaces: mx and lib. The lt;lib:Tree gt; notation means that we are planning to use a Tree component from the namespace that we called lib. As you can guess, I assume that you are going to program this Tree component in the ActionScript “s package com.enron.controls and will have to provide either the code of our Tree component or the SWC library that includes this Tree.

The namespace URI tells Flex where to look for the file implementing this component. You can either create a subdirectory com/enron/controls in the application “s directory, or preferably keep it in a separate location that is included in the classpath of your application (read about flex-config.xml file and the source-path tag in Flex documentation). Since we “ve defined two namespaces here, we can use components available in any of them.

You can also specify so-called local namespace using notations like xmlns= rdquo;* rdquo; or xmlns:mylocal= rdquo;* rdquo;, which tells Flex to look for components that are located in the same directory as MXML file or, in case of Flex Data Services, in the /WEB-INF/flex/user-classes directory.

Namespaces in ActionScript 3

Namespaces in ActionScript 3 as well as in MXML are used to control/limit the scope (visibility) of the methods, properties or constants. They are also used to avoid naming conflicts in cases if you create your own custom components that may have the same names as the Flex Framework or other vendor “s counterparts.

You can think of access control keywords public, private, protected and internal as a built-in name spaces. If a method has been declared as

protected calculateTax(){}

you can say that the method calculateTax() has a protected namespace. But AS3 allows you to define your own namespaces to be used instead of these standards language qualifiers.

To introduce your own namespace, you need to perform the following steps:

– Declare a namespace

– Apply the namespace

– Reference the namespace

Let “s write a simple program for an accountant who calculates taxes, but customers that belong to mafia should pay only half of the amount. To do this, we “ll start with declaring two namespaces called regular and soprano. This is the content of the file

package com.enron.namespaces {

public namespace soprano= “ “;


Please note that the use of a URI in the namespace declaration is optional. The listing below does not use any explicit URI, but the compiler will generate one. This is how the namespace called regular may look like (it ” defined in the ActionScript file

package com.enron.namespaces {

public namespace regular;


To apply the namespaces, we “ll define a class Tax with two methods calcTax() that will differ by the namespace access attribute and by the amount of “calculated rdquo; tax. The ActionScript class Tax may look like this:


import com.enron.namespaces.*;

public class Tax


regular static function calcTax():Number{

return 3500;


soprano static function calcTax():Number{

return 1750;




The testing class TextTax looks like this:

package com.enron.test


import com.enron.namespaces.*;


import mx.controls.Alert;

use namespace regular;

// use namespace soprano;

public class TestTax


public static function myTax():void {

var tax:Number;

tax=Tax.calcTax(); “Your tax is “+ tax, “Calculation complete “);




Since we apply the namespace for the regular customer, s/he will have to pay the tax amount of $3500. The MXML code that uses TestTax is shown below.

lt;?xml version= “1.0 ” encoding= “utf-8 “? gt;

lt;mx:Application xmlns:mx= “

layout= “absolute ” creationComplete= “initApp(); ” gt;

lt;mx:Script gt;


import com.enron.test.TestTax;

public function initApp():void {



]] gt;

lt;/mx:Script gt;

lt;/mx:Application gt;

The output of this program looks as in a screenshot below.

Switch to another namespace by changing the use statement to look like

use namespace soprano;

and the amount of tax to pay will by substantially lower. Besides the directive use that affects the entire block of code, AS3 allows more fine grained notation to refer to a specific namespace with a name qualifier (a double colon). In our example, this may look like this:

tax = Tax.soprano::calcTax();

This is the output of the TextTax.mxml for regular customers. For obvious reasons I do not show the output of this program for Soprano family members.

Using namespaces provides additional means of the visibility control (especially if you have something to hide). The methods, class properties of the constants can be physically located in different packages, but marked with the same namespace qualifier, and a one-line namespace change can engage a completely different set of methods/properties across the entire application.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s