Pergunta

I'm in the process of learning XHTML. If my understanding is correct, XHTML follows strict syntax. So I created this markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>    
        <title>XHTML page</title>
<!--    </head> -->
    <body>
        This is the body
    </body>
</html>

I've commented the ending head tag to check the strictness of XHTML. When I saved this file with .xhtml extension it showed an error. But when I saved it with .html extension browser was very forgiving and rendered the markup irrespective of the missing head tag.

So my doubt is, is it the extension(.xhtml) what makes a file actually xhtml and gives it strict syntax behavior? Shouldn't it be the DOCTYPE that determine whether its an xhtml or not?

From what I read somewhere, in HTTP, file extension doesn't really matter and its the header type that matters.

Foi útil?

Solução

When served from a web server, it's the mime type (aka content type) that determines whether the browser treats the document as XHTML or HTML. Browsers never use the DOCTYPE to determine whether the page is XHTML or not (but pre-HTML5 validators do).

When the file is read locally from the file system by the browser, it does indeed use the filename extension to decide whether it's XHTML or HTML.

Sometimes, but by no means always, servers are set up such that if the client requests a url with ".xhtml" at the end of it, the document will be served with an XHTML (e,g, application/xhtml+xml) mime type.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top