3

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" ?>
kjhughes
  • 106,133
  • 27
  • 181
  • 240

2 Answers2

3

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>
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 3
    I didn't understand what you said.He has asked why should we use XML instead of XHTML.Is it mandatory to use XML declaration,Please explain briefly .So that we can understand properly. – Vinay Guru Dec 23 '15 at 15:15
  • 3
    This is what we juniors excepted from seniors like you.In further please right briefly,so that everyone can understand. – Vinay Guru Dec 23 '15 at 18:39
  • 2
    You could add that the "XHTML declaration" follows after the xml declaration, in the form of the DOCTYPE, and _that_ is what defines the file as XHTML as well as describing the version. – Mr Lister Dec 23 '15 at 22:02
  • 2
    Good idea. I've added a note about DOCTYPE being where XHTML is declared and shown an example as well. Thanks. – kjhughes Dec 23 '15 at 22:39
1

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164