Bug #4401


HTML serialization: position of DOCTYPE declaration

Added by Michael Kay over 2 years ago. Updated 4 months ago.

Start date:
Due date:
% Done:


Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:


The W3C serialization specification says that with the HTML output method, the DOCTYPE declaration should appear "immediately before" the first start-element tag. That implies it should appear after any opening comments and processing instructions. I'm pretty sure we output it before any opening comments and PIs.

Need to check against the HTML spec to see what makes most sense.

Note also, we've had a couple of people trying to output PHP using the HTML output method, and stumbling over the fact that the HTML output method outputs PIs with a closing ">" rather than "?>". We've also had requests to suppress the DOCTYPE output for HTML-5.

Actions #1

Updated by Michael Kay over 2 years ago

  • Description updated (diff)
Actions #2

Updated by Michael Kay over 2 years ago

I've confirmed that we output the DOCTYPE before any comments using the simple XQuery command line

-qs:"<!--hey!-->,<html><head><title>Howdy</title></head><body>How are you?</body></html>" !method=html !html-version=5.0

I can't think of any good reason why the serialization spec says what it does, but it feels like a deliberate statement rather than something that slipped in by accident.

The HTML5 spec allows comments both before and after the DOCTYPE declaration, and there doesn't seem to be any strong reason for preferring one over the other. (Processing instructions aren't defined in the HTML syntax of HTML5, though the recovery features in the parser will cause them to be treated as comments.)

Actions #3

Updated by Michael Kay over 2 years ago

Further observation: the requirement to output the DOCTYPE "immediately before the first element" has been there since XSLT 1.0.

Moreover, the same requirement applies to the XML output method. (And for XML, this is what Saxon currently does).

For HTML, added test case output-0233

Actions #4

Updated by Michael Kay 4 months ago

  • Status changed from New to Closed

This appears to have been fixed at some point; the test case is working in both the 10.x and 11.x branches.

Please register to edit this issue

Also available in: Atom PDF