Entry
How does E4X deal with XML with namespaces?
How does E4X deal with XML with namespaces?
Mar 24th, 2009 17:09
chat alarab, Martin Honnen,
E4X is fully namespace aware: to deal with XML with namespaces two new
native objects, QName (for _Q_ualified _N_ame), and Namespace are added.
An XML object representing an element or attribute has an internal name
property which is a QName object having two properties, localName and
uri. For an element in no namespace the uri property has as its value
the empty string '', otherwise the uri property has as its value the
namespace URI:
var element = <p>Kibology for all.</p>;
var elementName = element.name();
alert(elementName instanceof QName); // show 'true'
alert(elementName.localName); // shows 'p'
alert(elementName.uri); // shows ''
element = <p xmlns="http://www.w3.org/1999/xhtml">Kibology for all.</p>;
elementName = element.name();
alert(elementName.localName); // shows 'p'
alert(elementName.uri); // shows 'http://www.w3.org/1999/xhtml'
element = <xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml">Kibology
for all.</xhtml:p>;
elementName = element.name();
alert(elementName.localName); // shows 'p'
alert(elementName.uri); // shows 'http://www.w3.org/1999/xhtml'
Contrary to the W3C DOM where namespace attributes in XML markup (e.g.
<element xmlns="someURI"> or <element xmlns:prefix="someURI") are also
represented as attributes in the object model in E4X namespace
attributes are not modelled as attributes in the object model but as
Namespace objects which are declared for an object:
var element = <p xmlns="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink">Kibology for all.</p>;
var attributeList = element.@*;
// namespace attributes in the markup not modelled as attributes as
alert(attributeList.length()); // shows 0
var namespaceDeclarations = element.namespaceDeclarations();
alert(namespaceDeclarations);
// shows 'http://www.w3.org/1999/xhtml,http://www.w3.org/1999/xlink'
For convenience to easily deal with XML with namespaces you can declare
a default namespace for the current scope (e.g. function scope or global
scope) using the newly introduced statement
default xml namespace = expression;
The right hand side should evaluate to a string with a URI or to a
Namespace object and the result of this statement is that all elements
created in the current scope will be in the default namespace if they
have no prefix in their name:
var p1 = <p>Kibology for all.</p>;
alert(p1.name().uri); // shows '';
// set default namespace
default xml namespace = 'http://www.w3.org/1999/xhtml';
var p2 = <p>Kibology for all.</p>;
alert(p2.name().uri); // shows 'http://www.w3.org/1999/xhtml';
// unset default namespace
default xml namespace = '';
var p3 = <p>Kibology for all.</p>;
alert(p3.name().uri); // shows '';
http://www.ksa-123.com
http://www.ksa-2000.com
http://www.chat-kuwait.com
http://www.vip-kuwait.com
http://www.chat-3rb.com
http://www.vip-3rb.com
http://www.3rb-chat.com
http://www.vipgulf.com
http://www.chat-gulf.com
http://www.vip-gulf.com