Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default namespace for HTML / HTML5? [closed]

According to this page by some user named "w3c" the default namespace for HTML is:

http://www.w3.org/1999/xhtml

He's obviously wrong since xhtml was used for a failed XML based HTML4 standard. What is the correct namespace I should use?

Background: I'm writing an app that uses XML. I want to be able to save data on the XML node using namespaces. For example,

<s:Button width="100" height="100" html:color="blue" color="black" /> 

The XML parser needs a namespace for the "html" prefix to be valid.

like image 667
1.21 gigawatts Avatar asked Dec 22 '15 02:12

1.21 gigawatts


2 Answers

As documented by the W3C, who are the body behind both the XML and HTML specifications (WHATWG has a separate HTML 5 spec, but while it in some ways out of sync with the W3C spec, WHATWG does still consider it the W3C's role to bring standardise a spec as a REC), the namespace for HTML when used with an XML serialisation (which is sometimes refered to as XHTML) is http://www.w3.org/1999/xhtml.

This namespace covers all versions of HTML with such XML serialisations that have been specified so far including XHTML 1.0 and 1.1 which were in considerable use for over a decade, XHTML 2.0 which introduced several modular ideas but which was arguably of more import as an incubator for several ideas than as an implemented version, and "HTML5 serialized as XML" which is sometimes called XHTML5.

Since the other body with a horse in the race as to just how HTML 5 is specified is WHATWG, you may note that they also say that if you are serialising HTML5 as XML then you must use the namespace http://www.w3.org/1999/xhtml and with the other serialisation either not use a namespace, or use that one. Per https://wiki.whatwg.org/wiki/FAQ#What_is_the_namespace_declaration.3F:

In XHTML, you are required to specify the namespace.

<html xmlns="http://www.w3.org/1999/xhtml">

In HTML, the xmlns attribute is currently allowed on any HTML element, but only if it has the value “http://www.w3.org/1999/xhtml“. It doesn’t do anything at all, it is merely allowed to ease migration from XHTML1. It is not actually a namespace declaration in HTML, because HTML doesn’t yet support namespaces. See the question will there be support for namespaces in HTML.

The next FAQ is also relevant here:

HTML is being defined in terms of the DOM and during parsing of a text/html all HTML elements will be automatically put in the HTML namespace, http://www.w3.org/1999/xhtml. However, unlike the XHTML serialization, there is no real namespace syntax available in the HTML serialization (see previous question). In other words, you do not need to declare the namespace in your HTML markup, as you do in XHTML. However, you are permitted to put an xmlns attribute on each HTML element as long as the namespace is http://www.w3.org/1999/xhtml.

In addition, the HTML syntax provides for a way to embed elements from MathML and SVG. Elements placed inside the container element math or svg will automatically be put in the MathML namespace or the SVG namespace, respectively, by the parser. Namespace syntax is not required, but again an xmlns attribute is allowed if its value is the right namespace.

In conclusion, while HTML does not allow the XML namespace syntax, there is a way to embed MathML and SVG and the xmlns attribute can be used on any element under the given constraints, in a way that is reasonably compatible on the DOM level.

like image 139
Jon Hanna Avatar answered Sep 22 '22 22:09

Jon Hanna


XHTML

The following default namespace declaration is required in XHTML for strictly conforming documents:

<html xmlns="http://www.w3.org/1999/xhtml">

The root element of the document must contain an xmlns declaration for the XHTML namespace [XMLNS]. The namespace for XHTML is defined to be http://www.w3.org/1999/xhtml

HTML5

The same default namespace declaration is optional in HTML5:

<html xmlns="http://www.w3.org/1999/xhtml">

... you do not need to declare the namespace in your HTML markup, as you do in XHTML. However, you are permitted to put an xmlns attribute on each HTML element as long as the namespace is http://www.w3.org/1999/xhtml

like image 25
kjhughes Avatar answered Sep 18 '22 22:09

kjhughes