75

What's the real difference between those two? I mean real, essential difference. What's the future holding for regular createElement?

Svg is xml, not html. I get that. So we use createElementNS(ns_string, 'svg') And then setAttributeNS(null,,). Why? Why not setAttributeNS('my_ns',,)?

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

What's 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 spec. but I'm still puzzled.

Wayne
  • 59,728
  • 15
  • 131
  • 126
CoR
  • 3,826
  • 5
  • 35
  • 42
  • 2
    If SVG also had a, say, `a` element, why would we assume it has the same role as the familiar HTML anchor element with the same tag name? Maybe, in SVG, it would refer to something else entirely, more fitting to the nature of SVG? Then all you need to do is ask yourself -- which `a` element would you get if you did `document.createElement("a")` in your script? See, without namespaces, the name of an element is entirely ambigous. – Armen Michaeli Aug 08 '17 at 11:46

1 Answers1

62

To understand the problem namespaces are trying to solve, consider file extensions. 3-letter file extensions have done a really bad job of describing the content of files. They're ambiguous and don't carry version info. XML namespaces use a larger space of strings, URIs, to solve the same problem, and use short prefixes so you can succinctly mix multiple kinds of XML in the same document.

What's the purpose of namespace if there is only one name space?

There are many namespaces used to identify different kinds of XML, and different versions of those kind.

SVG and MathML are two kinds of XML each with their own namespaces that can be embedded in HTML5, and they often use XLink, another XML namespace. Many other XML schemas, with corresponding namespaces, are used for passing messages between clients and servers and for data storage.

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,,). Why? Why not setAttributeNS('my_ns',,)???

You should probably try to consistently use setAttributeNS with a namespace URI when using createElementNS with a namespace URI.

XML was defined in multiple steps. The first version of the spec said nothing about namespaces but left enough syntax so that XML with namespaces could be specified on top of XML without namespaces by using prefixes and special xmlns attributes. The XML specification says:

"The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character."

XML namespaces let XML processing applications know what they're dealing with, and allow multiple kinds of XML to be mixed together in the same document.

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

This includes the year that version of SVG was standardized, 2000, so it carries useful information.

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

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 2
    Mmmm, so just is not enough. It has to have a proper xml namespace to be 'real' svg... Why we don't use namespaces for regular dom elements? Like
    – CoR Nov 17 '11 at 20:07
  • 2
    @CoR, `` may be enough for an HTML 5 parser, and an XML renderer could use context to tell that it is an SVG root element, but if you have an XML parser that is dealing with many different kinds of content, namespaces should be used. Please see the first paragraph I just added to my answer. – Mike Samuel Nov 17 '11 at 20:14
  • 3
    @CoR, You *do* use namespaces for regular DOM elements, but only when they're not in the default namespace. In an XHTML file the default namespace is implicit, in an SVG or XML file, it's not. – zzzzBov Nov 17 '11 at 20:16
  • @zzzzBov, I think I agree but I'm unsure what you mean by "XML" in "in an SVG or XML file, it's not." – Mike Samuel Nov 17 '11 at 20:26
  • @MikeSamuel, if I create an XML file with my own custom DTD, I can allow some elements to contain XHTML, but the XHTML will still need to be correctly namespaced to be valid within my custom format. – zzzzBov Nov 17 '11 at 20:30
  • 3
    @zzzzBov, I agree. I am confused by "in an XHTML file ..., [but] in an SVG or XML file" because SVG and XHTML are XML. I would find "Cats are furry, but frogs and animals are not" confusing for the same reason. – Mike Samuel Nov 17 '11 at 20:32
  • 2
    @MikeSamuel, good point. let me redact that to say: "SVG or **any other XML** file" I meant `XML` in the generic in that the file name would end with `.xml`. – zzzzBov Nov 17 '11 at 20:34