Generating HTML5 DOCTYPE with XSLT

Having trouble installing <oXygen/>? Got a bug to report? Post it all here.
sarcanon
Posts: 25

Generating HTML5 DOCTYPE with XSLT

Sun Mar 18, 2012 6:26 am

I have been trying (and failing) all evening to generate proper HTML5 documents using XSLT, with various combinations of output methods and @doctype-system and @doctype-public attribute values. But no matter what I try, I cannot produce a simple DOCTYPE like this:

Code: Select all

<!DOCTYPE html>

Might someone please be able to tell me what I am doing wrong?

FWIW, I am using Oxygen 13.2 build 2012030716 and the Saxon-HE 9.3.0.5 processor on Windows 7 (64). And the beginning of my stylesheet looks like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:t="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="t" version="2.0">

    <xsl:output method="html" encoding="UTF-8"/>

Many thanks in advance.
Radu
Posts: 5376

Re: Generating HTML5 DOCTYPE with XSLT

Mon Mar 19, 2012 9:44 am

Hi,

You could use the xsl:text element to output the empty DOCTYPE declaration like:

Code: Select all

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xhtml"/>
    <xsl:template match="/">
        <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html></xsl:text>
        <html xmlns="http://www.w3.org/1999/xhtml">
      ..........................
        </html>
    </xsl:template>
</xsl:stylesheet>


Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
sarcanon
Posts: 25

Re: Generating HTML5 DOCTYPE with XSLT

Tue Mar 20, 2012 8:49 am

Dear Radu:

That works. Thank you very much.

But I am a little surprised that there isn't a more elegant solution within XSLT to address what must be a common requirement. I surely cannot be the only person who wants to generate HTML5 docs in this manner.

In any event, thanks again for the solution.
Radu
Posts: 5376

Re: Generating HTML5 DOCTYPE with XSLT

Tue Mar 20, 2012 9:47 am

Hi,

From what I've tested a Web Browser always interprets opened HTML files as HTML 5 so you do not need to set that <!DOCTYPE html> in the file in order for the browser to interpret it as HTML 5.

From an XML point of view a syntax like this: <!DOCTYPE html> without specifying a public or system ID is meaningless so this is probably why this XSLT construct:

Code: Select all

<xsl:output method="xml" doctype-system="" doctype-public=""></xsl:output>


will ignore generating the DOCTYPE altogether.

Here is a list of differences between HTML 5 and 4:

http://www.w3.org/TR/html5-diff/

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
jelovirt
Posts: 85
Location: Helsinki, Finland

Re: Generating HTML5 DOCTYPE with XSLT

Thu Mar 22, 2012 9:53 am

HTML5 has a DOCTYPE legacy string:

Code: Select all

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


That can be output with

Code: Select all

<xsl:output doctype-system="about:legacy-compat"/>
jannylun
Posts: 16

Re: Generating HTML5 DOCTYPE with XSLT

Tue Mar 25, 2014 4:38 pm

I agree with jelovirt, the preferred way from XSLT is to use the proper legacy string. However, this does currently not validate in Oxygen 15.

Image
Radu
Posts: 5376

Re: Generating HTML5 DOCTYPE with XSLT

Tue Mar 25, 2014 4:57 pm

Hi Jan,

There is a problem with the approach:

Code: Select all

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


Basically Oxygen properly detects the document as being XHTML 5 and it uses XHTML 5 Relax NG Schemas to validate the XML document.
But as the document has a SYSTEM identifier specified on the DOCTYPE, any XML parser (like the Xerces parser used by the Relax NG validation) will try to expand that SYSTEM reference to a resource before the XML is parsed (XML might contain entity references or default attribute values which come from the DTD). And the XML parser fails to do so.

As a workaround you could create an XML catalog file with the content:

Code: Select all

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <system systemId="about:legacy-compat" uri="test.dtd"/>
</catalog>


which maps the system ID to a file test.dtd which is empty. And then add the XML catalog to the catalogs list in the Oxygen Preferences XML / XML Catalog page.

In my opinion any HTML document without a specific DOCTYPE is considered HTML 5 by a web browser so you do not need to set any particular DOCTYPE declaration to it.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “Common Problems”

Who is online

Users browsing this forum: No registered users and 1 guest