Set HTML5 doctype with XSLT
How would I cleanly set the doctype of a file to HTML5 <!DOCTYPE html>
via XSLT (in this case with collective.xdv)
The following, which is the best my Google foo has been able to find:
<xsl:output
method="html"
doctype-public="XSLT-compat"
omit-xml-declaration="yes"
encoding="UTF-8"
indent="yes" />
produces:
<!DOCTYPE html PUBLIC "XSLT-compat" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Solution 1:
I think this is currently only supported by writing the doctype out as text:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
</html>
</xsl:template>
</xsl:stylesheet>
This will produce the following output:
<!DOCTYPE html>
<html>
</html>
Solution 2:
To use the simple HTML doctype <!DOCTYPE html>
, you have to use the disable-output-escaping
feature: <xsl:text disable-output-escaping="yes"><!DOCTYPE html></xsl:text>
. However, disable-output-escaping
is an optional feature in XSLT, so your XSLT engine or serialization pipeline might not support it.
For this reason, HTML5 provides an alternative doctype for compatibility with HTML5-unaware XSLT versions (i.e. all the currently existing versions of XSLT) and other systems that have the same problem. The alternative doctype is <!DOCTYPE html SYSTEM "about:legacy-compat">
. To output this doctype, use the attribute doctype-system="about:legacy-compat"
on the xsl:output
element without using a doctype-public
attribute at all.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compat"/>
...
<html>
</html>
</xsl:stylesheet>
Solution 3:
<xsl:output
method="html"
doctype-system="about:legacy-compat"
encoding="UTF-8"
indent="yes" />
this outputs
<!DOCTYPE html SYSTEM "about:legacy-compat">
this is modified as my fix to http://ukchill.com/technology/generating-html5-using-xslt/
Solution 4:
With Saxon 9.4 you can use:
<xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes" />
This generates:
<!DOCTYPE HTML>