CreateElement vs createElementNS

What is the real difference between the two? I mean the real, significant difference. What future holds regular createElement ?

Svg is xml, not html. I understand. So we use createElementNS(ns_string, 'svg') and then setAttributeNS(null,,) . What for? Why not setAttributeNS('my_ns',,) ?

Why should ns_string be http://www.w3.org/2000/svg and not some random string? What is the purpose of a namespace if there is only one namespace?

What is the purpose of ns in regular html? Should I change all instances of createElement to createElementNS in my existing code?

I am reading the DOM-Level-2 specification. but I'm still puzzled.

+45
javascript dom html5 namespaces
Nov 17 '11 at 19:40
source share
1 answer

To understand the problem the namespace is trying to solve, consider file extensions. 3-letter file extensions did a very poor job of describing the contents of the files. They are ambiguous and do not contain version information. XML namespaces use a larger string space, URI, to solve the same problem and use short prefixes so that you can briefly combine several kinds of XML in one document.

What is the purpose of a namespace if there is only one namespace?

There are many namespaces used to identify different types of XML and different versions of this kind.

SVG and MathML are two types of XML, each of which has its own namespaces that can be embedded in HTML5, and they often use XLink, another XML namespace. Many other XML schemas with associated namespaces are used to transfer messages between clients and servers and to store data.

XHTML is an attempt to express HTML as valid XML. It has its own namespace.

So we use createElementNS (ns_string, 'svg') and then setAttributeNS (null,). What for? Why not setAttributeNS ('my_ns',) ???

You should probably consistently use setAttributeNS with the namespace URI when using createElementNS with the namespace URI.

XML was defined in several steps. The first version of the specification did not say anything about namespaces, but left enough syntax for XML with namespaces to be specified over XML without namespaces using prefixes and special xmlns attributes. The XML specification says:

"Namespaces in the XML Recommendation [XML Names] assign values ​​to names containing colon characters. Therefore, authors should not use the colon in XML names other than namespace purposes, but XML processors should accept the colon as the name character."

XML namespaces let XML processing applications know what they are dealing with, and allow you to mix several types of XML in a single document.

Why should ns_string be " http://www.w3.org/2000/svg "

This includes the year the SVG version was standardized, 2000, so it contains useful information.

When used with xmlns:svg it also lets the browser know that the svg: prefix means SVG, and not some other XML dialect.

+37
Nov 17 '11 at 19:52
source share