Entry
How do I use the method namespace?
How do I use the method namespace?
Apr 11th, 2005 03:33
Martin Honnen,
The method namespace takes one optional argument, a prefix.
If no argument is passed to the method then it returns the namespace
associated with the qualified name of this XML object.
If a prefix is passed to the method then it looks for a matching
namespace in the in scope namespace of this XML object and returns it
when found, otherwise undefined is returned.
Here are some examples of calling the method with no argument:
var p = <xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml">Kibology
for all.</xhtml:p>;
var namespace = p.namespace();
alert('Prefix "' + namespace.prefix + '" bound to URI "' +
namespace.uri + '"');
// shows 'Prefix "xhtml" bound to URI "http://www.w3.org/1999/xhtml"'
var p = <p xmlns="http://www.w3.org/1999/xhtml">Kibology for all.</p>;
var namespace = p.namespace();
alert('Prefix "' + namespace.prefix + '" bound to URI "' +
namespace.uri + '"');
// shows 'Prefix "" bound to URI "http://www.w3.org/1999/xhtml"'
var god = <god>Kibo</god>;
var namespace = god.namespace();
alert('Prefix "' + namespace.prefix + '" bound to URI "' +
namespace.uri + '"');
// shows 'Prefix "" bound to URI ""'
Here are some examples of calling the method with a prefix argument:
var p = <xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink">Kibology
for all.</xhtml:p>;
var namespace = p.namespace('xhtml');
alert('Prefix "' + namespace.prefix + '" bound to URI "' +
namespace.uri + '"');
// shows 'Prefix "xhtml" bound to URI "http://www.w3.org/1999/xhtml"'
var namespace = p.namespace('xlink');
alert('Prefix "' + namespace.prefix + '" bound to URI "' +
namespace.uri + '"');
// shows 'Prefix "xlink" bound to URI "http://www.w3.org/1999/xlink"'
var namespace = p.namespace('svg');
alert(namespace); // shows 'undefined'