javascript - What is an xml dom namespace and why do some DOM methods have NS at their end? -
why need write it? , why methods of dom have ns @ end, what's purpose of such methods?
namespaces meant resolve <tagname> conflicts
consider xml tree:
<aaa xmlns="http://my.org"> <bbb xmlns="http://your.org">hello</bbb> <bbb>hello</bbb> </aaa> the first <bbb> tag belongs namespace http://my.org
the other 1 belongs 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 can see 2 <namevalue> tags, 1 company's name, 1 refers chairman's name... conflict resolved using prefix!
another way write 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 don't specify prefix, defining default namespace
xmlns="http://default-namespace.org" xmlns:nondefault="http://non-default-namespace.org" means, example, descendant element <sometest> belongs http://default-namespace.org
<nondefault:anotherone> instead belongs http://non-default-namespace.org
why using urls namespace string? because identify certified source no risks of conflicts (a domain name can owned 1 single person) url put in xmlns attribute isn't downloaded or parsed somehow, it's string uniquely identifies tags namespace. use, @ same way, example string such xmlns="com.yourcompany.yournamespace"
so, dom methods such document.getelementbytagnamens() meant select elements of specific namespace
<?php // asking php here // page must served application/xml otherwise // getelementsbytagnamens 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 html!</p> <com:namevalue> microsoft </com:namevalue> <cha:namevalue> bill gates </cha:namevalue> <com:countryvalue> usa </com:countryvalue> <p>this 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> , <cha:name> alert("any namevalue: " + document.getelementsbytagname( "namevalue").length); // selects <com:name> alert("company namevalue: " + document.getelementsbytagnamens( "http://someschema.org/company", "namevalue").length); // selects <cha:name> alert("chairman namevalue: " + document.getelementsbytagnamens( "http://someschema.org/chairman", "namevalue").length); }; //]]> </script> </html>
Comments
Post a Comment