XML Namespace and Actionscript 3.0
As defined by the W3C , an XML namespace is a collection of XML elements and attributes identified by an Internationalized Resource Identifier (IRI); this collection is often referred to as an XML “vocabulary.”
IRI is usually an URI e.g. http://www.brupp.com. Let’s understand this concept briefly. Take this example:
<root xmlns:brupp="http://brupp.com/">
<data>
<title>Title</title>
<link>http://foofoo.com</link>
<brupp:link href="http://brupp.com/somelink">Some Data</brupp:link>
<description>description data</description>
...
</data>
...
...
</root>
The XML above has no default namespace. Now lets suppose the XML demands need of reuse of some other existing namespace “http:/brupp.com” which also has a <link> node. To avoid any ambiguity between the <link> node of both vocabulary XML namespace are added.
Why namespace? As described above it helps in leveraging different vocabulary of XML with same element and attribute name. Understand this way; you created a XML structure for a “Product” and “Customer”. Now you want to utilize these XML structure in a new “Database” XML. The problem: Both “Product” and “Customer” uses <id> node. Solution: Place them in different namespace (simply a unique string) and then use them as shown above xmlns:prod=”some namespace”.
In actionscript 3.0 you can access both< link> node without much hassle. See below a simple example created in Flex:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="732" height="434"
creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
namespace brupp = "http://brupp.com/";
use namespace brupp;
private var _objXML:XML =
<root xmlns:brupp="http://brupp.com/">
<data>
<title>Title</title>
<link>http://foo.com</link>
<brupp:link href="http://brupp.com/somelink" >some</brupp:link>
<description>description data</description>
</data>
</root>;
private function onCreationComplete():void
{
trace("default link " + _objXML.data.link);
}
]]>
</mx:Script>
</mx:Application>
The trace above will show both <link> nodes because we have told the compiler to “use namespace” brupp. If you comment the code line:
use namespace brupp;
and then run the application again; the “brupp” namespace <link> will not appear.
To add, if you only want to access the “brupp” namespace <link>, do this:
trace("brupp link " + _objXML.data.brupp::link);
If you liked this post, please spread the word. Share and cheer!!!.
If you need more information on above post drop a comment. Your comments are valuable and helps in molding the article so do share in.
Recent Comments