Shouldn't it be: Shouldn't it be: Shouldn't it be:

Why <?xml ... ?> rather than <?xhtml ...?> for XHTML?

137 Views Asked by At

In the beginning of an XHTML file, why do we use "xml" in the following construct:

<?xml version = "1.0" encoding = "utf-8" ?>

Shouldn't it be:

<?xhtml version = "1.0" encoding = "utf-8" ?>
2

There are 2 best solutions below

4
kjhughes On

The <?xml ...?> construct,

<?xml version = "1.0" encoding = "utf-8" ?>

is an XML declaration, and the xml part does not vary per type of XML file. It should be the same for an XHTML file (which is XML by definition).

Notes:

XHTML Prolog Example:

Per the W3C Recommended list of Doctype declarations, where you can find other examples as well:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>An XHTML 1.0 Strict standard template</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  </head>
  <body>
     <p>… Your HTML content here …</p>
  </body>
</html>
0
Michael Kay On

Q: Why do we use <?xml..?>?

A: Because that's what the spec says we must use.

Q: Why does the spec say that?

A: Because the relationship between XHTML and XML is that XHTML is a particular XML vocabulary. The XML declaration is there to give information to the XML parser (about the version of XML and the encoding of the file), and the XML parser handles any XML file regardless of what vocabulary is used.

It could have been designed differently, of course. But it wasn't.