In HTML with XML syntax, can one use elements with namespaces other than HTML, SVG and MathML?

Viewed 130

It has long been my understanding that one advantage of the XML syntax for writing HTML documents is that it also permits to include in HTML documents elements with a namespace that is not one of those specified by the HTML standard. For example, user-created elements in a user-created namespace describing books. But, reading the WhatWG specs, this seems to be forbidden, or I do not find how to do it (I mean, in a way that is conforming to the specs).

I am thinking about something similar to the following document.

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    …
  </head>
  <body>
    <h1>Title</h1>
    <book xmlns="http://example.com/my-book-namespace">
      …
    </book>
  </body>
</html>

I realize that of course the browser would not know how to render such elements coming from unknown namespaces, but this may not be a problem (I expect browsers would simply ignore such elements). For example, some javascript on the page could use such elements as data in order to generate HTML elements dynamically.

For example, this tutorial about Java Server Faces includes elements in the JSF namespace in what seems to be intended as a conforming XHTML page.

But the specs seems to imply that such an example would be non conforming, and even, that there may be no way of including such elements in an HTML document. “The exact allowed contents of each individual element depend on the content model of that element, as described earlier in this specification. Elements must not contain content that their content model disallows.” (elements); and the body element specifies only Flow content as Content model; which does not seem to permit using elements outside of those explicitly specified by the HTML specification.

So, if I insist in producing only spec-conforming HTML documents, should I refrain from including in an HTML document elements from a namespace different from the HTML, SVG and MathML namespaces?

1 Answers

While WHATWG's HTML spec indeed states that

Elements must not contain content that their content model disallows.

as you cited, chapter 13.2.6.4.7 (The "in body" insertion mode) states the actual action that HTML parsers are supposed to perform on your example doc:

Any other start tag:
Insert an HTML element for the token

(https://html.spec.whatwg.org/#parsing-main-inbody).

And even though the parsing rules given in chapter 13 grossly mix error recovery and SGML-style tag inference actions (the latter only applying to HTML serialization), in the absence of other rules they should apply to XML content as well (or none of the grammar rules could apply to XML at all).

Consequently, browsers won't ignore your book element but will treat it according to 13.2.6.1 Creating and inserting nodes - Insert a foreign element. If your doc had actual book content such as in the following example,

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Test</title>
  </head>
  <body>
    <h1>Title</h1>
    <book xmlns="http://example.com/my-book-namespace">
      <descr xmlns="http://example.com/my-book-namespace">
        some text
      </descr>
    </book>
  </body>
</html>

browsers will render the descr text content as unstyled content, but you could style it using CSS @namespace rules like so:

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Test</title>
   <style>
     @namespace url(http://example.com/my-book-namespace);
     descr { color: blue }
   </style>
  </head>
  <body>
    <h1>Title</h1>
    <book xmlns="http://example.com/my-book-namespace">
      <descr xmlns="http://example.com/my-book-namespace">
        some text
      </descr>
    </book>
  </body>
</html>
Related