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:
xmlns:namespaceName="URI"For example:
<pictures xmlns:ns="https://coursesweb.net"> <ns:image>img1.jpg</ns:image> <image>img2.png</image> </pictures>- "ns" represents the name of the Namespace, it can be any word.
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.
// Variable with XML data var galery:XML = <pictures xmlns:ns="https://coursesweb.net"> <ns:image>img1.jpg</ns:image> <image>img2.png</image> </pictures>; trace(galery.image[0].namespace('ns')); // https://coursesweb.net trace(galery.image[1].namespace('ns')); // Error #1010: A term is undefined
new Namespace("prefix", "value_uri");Then use this instance as argument in the "addNamespace()" method.
var name_var:Namespace = new Namespace(); name_var.prefix = "namespaceName"; name_var.uri = "value_uri";
// Variable with XML data var test:XML = <root> <tag>Text elm1</tag> <tag atr="val">Elm 2</tag> </root>; // define the instance with the namespace var ns1:Namespace = new Namespace('ns', 'coursesweb.net/flash'); // Add the namespace in the XML object (in root) test.addNamespace(ns1); test.tag[0].setNamespace(ns1); // Sets "ns" to the first <tag> // Sets "ns" at the "atr" attribute in the second <tag> test.tag[0].@atr.setNamespace(ns1); // Or: test.children()[1].@atr.setNamespace(ns1); trace(test);- This example displays in Output the following XML format:
<root xmlns:ns="coursesweb.net/flash"> <ns:tag>Text elm1</ns:tag> <tag ns:atr="val">Elm 2</tag> </root>- 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".
XMLobject.instanceNamespace::nodeTo understand it better, see the following example:
// Variable with XML data var galery:XML = <pictures xmlns:ns="https://coursesweb.net"> <ns:image>img1.jpg</ns:image> <ns:image ns:title="Img 2">img2.png</image> </pictures> // 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).
galery.name_s::image[1].@name_s::title = 'Another title';- Modify the value of the "title" attribute in the second <image> element.
delete galery.name_s::image[0];- Delete the first <image> element.
// Variable with the XML data var test:XML = <root xmlns:ns="coursesweb.net/flash"> <ns:tag>Text elm1</ns:tag> <tag ns:atr="val">Elm 2</tag> </root> // 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
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
#id { font-weight: 800; }
function someFunction() { alert("CoursesWeb.net"); } setInterval("someFunction()", 2000);
$vname = 8; echo $vname;