Reputation: 779
why do I need to write it? and why do some methods of DOM have NS at their end, what's the purpose of such methods?
Upvotes: 2
Views: 181
Reputation:
Namespaces are meant to resolve <tagname>
conflicts
Consider this XML tree:
<aaa xmlns="http://my.org">
<bbb xmlns="http://your.org">hello</bbb>
<bbb>hello</bbb>
</aaa>
the first <bbb>
tag belongs to the namespace http://my.org
the other one belongs to the namespace http://your.org
Another example
<company xmlns="http://someschema.org/company"
xmlns:chairman="http://someschema.org/chairman">
<nameValue>Microsoft</nameValue>
<chairman:nameValue>Bill Gates</chairman:nameValue>
<countryValue>USA</countryValue>
</company>
There you can see two <nameValue>
tags, one is the company's name,
one refers to the chairman's name... but that conflict is resolved using a prefix!
Another way to write that is:
<com:company
xmlns:com="http://someschema.org/company"
xmlns:cha="http://someschema.org/chairman">
<com:nameValue> Microsoft </com:nameValue>
<cha:nameValue> Bill Gates </cha:nameValue>
<com:countryValue> USA </com:countryValue>
</com:company>
So, if you don't specify a prefix, you are defining the default namespace
xmlns="http://default-namespace.org"
xmlns:nondefault="http://non-default-namespace.org"
Means, for example, that a descendant element <sometest>
belongs to http://default-namespace.org
<nondefault:anotherone>
instead belongs to http://non-default-namespace.org
Why using URLs as a namespace string? because they identify a certified source with no risks of conflicts (a domain name can be only owned by one single person)
so the URL you put in the xmlns
attribute isn't downloaded or parsed somehow, it's just a string that uniquely identifies your tags namespace.
You could use, at the same way, for example a string such as xmlns="com.yourcompany.yournamespace"
So, DOM methods such as document.getElementByTagNameNS() are meant to select elements of a specific namespace
<?php
// asking php some help here
// page must be served as application/xml otherwise
// getElementsByTagNameNS will not work !!!
header("Content-Type: application/xml; charset=UTF-8");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<com:company
xmlns:com="http://someschema.org/company"
xmlns:cha="http://someschema.org/chairman">
<p>this is html!</p>
<com:nameValue> Microsoft </com:nameValue>
<cha:nameValue> Bill Gates </cha:nameValue>
<com:countryValue> USA </com:countryValue>
<p>this is html!</p>
</com:company>
<script>
//<![CDATA[
window.onload = function(){
alert("html paragraphs: " + document.getElementsByTagNameNS(
"http://www.w3.org/1999/xhtml", "p").length);
// selects both <com:name> and <cha:name>
alert("any nameValue: " + document.getElementsByTagName(
"nameValue").length);
// selects only <com:name>
alert("company nameValue: " + document.getElementsByTagNameNS(
"http://someschema.org/company", "nameValue").length);
// selects only <cha:name>
alert("chairman nameValue: " + document.getElementsByTagNameNS(
"http://someschema.org/chairman", "nameValue").length);
};
//]]>
</script>
</html>
Upvotes: 3