TMCom: Now Real XHTML 5

As mentioned in a previous post, my site has gone to the HTML 5 doctype. I had come from XHTML 1.0 and wanted to continue with the XML syntax of HTML 5, but my site wouldn’t validate with the XML declaration. I recently remembered that I had been serving my site with the mime-type “text/html”, which is allowed in XHTML 1.0 transitional. HTML 5 got stricter, and if you want to use the XML syntax, it must be served as “application/xhtml+xml” or “application/xml”.

So I modified the doctype switcher I had made (mentioned in that previous post) to change the mime-type to “application/xhtml+xml” when the configuration doctype was set to “xhtml5”. But IE evidently cannot handle that mime-type, so I set up my switcher to output as “html5” for IE, but “xhtml5” for other browsers. I reset the doctype variable (now an attribute of a page object):

if($this->doctype == 'xhtml5' && strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
    $this->doctype = 'html5';

I send my “Content-type” header only for a doctype of “xhtml5”:

if($this->doctype == 'xhtml5')
    header("Content-type: application/xhtml+xml;charset=utf-8");

And, since I’ve removed the now optional “xmlns” and “xml:lang” attributes for the “head” tag in HTML 5, I make sure to put them back in for XHTML 5:

if($this->doctype != 'html5' && !defined('pagIsXSLT')): echo 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"'; endif;

Most other output is the same for HTML 5 and XHTML 5.

I have not actually verified this works in IE, since I don’t have access to it. I did verify that sending a user agent string from IE 7 does change the doctype properly though.

For my XML sitemap, I had been just sending it as XHTML 1.0 still, since XSLT doesn’t have an HTML 5 doctype for the “xsl:output” declaration. However, the comments on this post pointed to a solution: the doctype legacy string format. My “xsl:output” element now looks like this:

<xsl:output method="html" indent="yes" doctype-system="about:legacy-compat" />

It renders just fine in both iCab (WebKit) and Firefox. I, of course, haven’t tested this in IE.

[Update 2010-8-15] Updated the xsl:output declaration to reflect the changes to the recommendation as pointed out by Andreas Riedmüller. Had been:

<xsl:output method="html" indent="yes" doctype-public="XSLT-compat" />

Published by

Toby

I am a quiet person from Northeast Ohio. I work as a web developer. I like computers, music, and many other things.

9 thoughts on “TMCom: Now Real XHTML 5”

  1. Hello Toby,

    i think the right solotion is not to use doctype-public but doctype-system instead.

    this produces:

    which should be the right output for generators that cannot output HTML markup with the short DOCTYPE “”

    (see. 8.1.1 The DOCTYPE in the HTML5 editors draft)

    am I right?

    1. Now encoded, so you can see the code :)

      Hello Toby,

      i think the right solotion is not to use doctype-public but doctype-system instead.

      <xsl:output method="html" indent="yes" doctype-system="about:legacy-compat" />

      this produces:

      <!DOCTYPE html
      SYSTEM "about:legacy-compat">

      which should be the right output for generators that cannot output HTML markup with the short DOCTYPE

      (see. 8.1.1 The DOCTYPE in the HTML5 editors draft)

      am I right?

      1. Hmm, that’s weird, they must have changed that section. There are still blog posts to be found discussing the XSLT-Compat legacy doctype and a WHATWG thread quoting similarly to the section in the current draft but with XSLT-Compat instead.

        I will look into it a bit more and change it if this is indeed the new way.

      2. I changed this on my site. The W3C validator gives the warning “No DOCTYPE found! Checking XML syntax only.” It does for both doctypes, so it had to have been doing this before. Setting the doctype to HTML5 in the validator gives lots of errors, and there isn’t really any other better manual setting to choose. Not sure what this says in regards to validation, but it renders fine (now verified in IE, my layout gets messed up in 6 and 7 but still renders).

    1. Hmm, doesn’t seem to help either. I am validating the file served as XML with reference to an XSLT converting it to XHTML5. The file is an XML sitemap. When served as mime-type “text/html” (proper for an XML sitemap) totalvalidator simply says it is “not an (x)html page”. Served as “application/xhtml+xml” it points to the many unrecognized elements. I imagine there’s really not a validator that will directly validate the XHTML result of an XSLT transform anyway, and seemingly the only way to really validate XML is to see if browsers or other parsers parse it without throwing an error. I can’t really directly grab the output of the transformation as applied by a browser to validate that by copy and paste.

      That totalvalidator seems somewhat nice though, with accessibility checking built in.

Leave a Reply

Your email address will not be published. Required fields are marked *